简体   繁体   中英

Resuming Android Activity

I have a simple application, where I created a SurfaceView class, then in the main activity, I created an object of the class, then added this SurfaceView to a relative layout, and set the content view to the relative layout.

package com.my.game;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;

public class ShootLetters extends Activity {

    private Panel panel;
    private int newWidth;
    private int newHeight;

    private AdView adView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bitmap bg = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.shooting_background);
        ;

        int width = bg.getWidth();
        int height = bg.getHeight();

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        newWidth = metrics.widthPixels;

        newHeight = metrics.heightPixels;
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBackGround = Bitmap.createBitmap(bg, 0, 0, width, height,
                matrix, false);

        Window win = getWindow();
        Drawable d = new BitmapDrawable(resizedBackGround);
        win.setBackgroundDrawable(d);

        panel = new Panel(this, newWidth, newHeight);

        panel.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    panel.setClickX(event.getX());
                    panel.setClickY(event.getY());
                }
                return true;
            }

        });

        adView = new AdView(this, AdSize.BANNER, "admobidxxx222");

        RelativeLayout rl = new RelativeLayout(this);
        rl.addView(panel);
        rl.addView(adView);

        setContentView(rl);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
    }

    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        panel.stopThread();
        panel = null;

        super.onDestroy();
    }

}

This works fine for starting and stopping the application.

The problem is when another activity comes on top of this one, it behaves in an unstable way (no need for details at this stage as sometimes it crashes and sometimes it works with missing layout objects).

I read that I should implement onPause() & onResume() methods to handle this. I only need pause the activity or resume it on resume. No persistent data required.

What should I put in which methods in this case?

Thanks

I guess its your Panel that is the surfaceview? I see you are doing something with its thread in onDestroy but that is not sufficient. You had a correct hunch about implementing onPause() and onResume() Now my idea(without being able to see all of your code) is that you are not dealing with the thread correctly. In your Panel class which extends surfaceview you should have something like these two functions:

    public void pause(){
        Loop = false;
        while (true){
            try{
                panelThread.join();//end it
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            break;
        }
        panelThread = null;
    }
    public void resume(){
        Loop = true;
        panelThread = new Thread(this);
        panelThread.start();
    }

Ignore the Loop part if you are not using a loop in your run() method.

Then, in your activity's onPause() and onResume() methods you call panel.pause() and panel.resume() respectively. This makes sure that the surface view thread is being handled in a safe way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM