Android - onPause 和 onResume

Android - onPause and onResume

我有随机绘制红色圆圈的代码。直到我在 BOTH 类 中都有暂停和恢复方法后,它才起作用。如果没有 pause 和 resume 方法,屏幕只会是黑色的,不会改变。为什么我需要 onPauseonResume 方法,为什么 类 都需要?

注释的代码是所有pause/resume方法。

public class RandomCircles extends Activity {

    MySurfaceView mySurfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView);
    }


 /*   @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mySurfaceView.onResumeMySurfaceView();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mySurfaceView.onPauseMySurfaceView();
    }*/

    class MySurfaceView extends SurfaceView implements Runnable{

        Thread thread = null;
        SurfaceHolder surfaceHolder;
        volatile boolean running = false;

        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Random random;

        public MySurfaceView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            surfaceHolder = getHolder();
            random = new Random();
        }

        /*public void onResumeMySurfaceView(){
            running = true;
            thread = new Thread(this);
            thread.start();
        }

        public void onPauseMySurfaceView(){
            boolean retry = true;
            running = false;
            while(retry){
                try {
                    thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }*/

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(running){
                if(surfaceHolder.getSurface().isValid()){
                    Canvas canvas = surfaceHolder.lockCanvas();
                    //... actual drawing on canvas

                    int x = random.nextInt(getWidth());

                    if(getWidth() - x < 100)
                        x -= 100;
                    else if(getWidth() - x > getWidth() - 100)
                        x += 100;

                    int y = random.nextInt(getHeight());

                    if(getHeight() - y < 100)
                        y -= 100;
                    else if(getHeight() - x > getHeight() - 100)
                        y += 100;

                    int radius;
                    radius = 100;
                    Paint paint = new Paint();
                    paint.setStyle(Paint.Style.FILL);
                    paint.setColor(Color.WHITE);
                    canvas.drawPaint(paint);
                    // Use Color.parseColor to define HTML colors
                    paint.setColor(Color.parseColor("#CD5C5C"));
                    canvas.drawCircle(x, y, radius, paint);

                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }

    }
}

前两个 onPause()onResume() 方法是 Activity 生命周期的一部分,并在 Activity 为 paused/resumed 时调用。

此图说明了 Android Activity 生命周期。您可以继续阅读 HERE

它与 Activity 中的其他 onResumeonPause 方法一起工作的原因是因为您调用了 SurfaceView onPauseMySurfaceView()onResumeMySurfaceView() 方法来自 Activity 中的各个方法。如果您不这样做,您的 SurfaceView 方法将永远不会被调用,因此也永远不会 stopped/started 线程。