Android - onPause和onResume

时间:2015-06-19 13:45:29

标签: android android-activity onresume onpause

我有随机绘制红色圆圈的代码。直到我在BOTH课程中暂停和恢复方法时才能工作。如果没有暂停和恢复方法,屏幕将只是黑色而不会改变。为什么我需要onPauseonResume方法以及为什么要在这两个类中?

注释代码是所有暂停/恢复方法。

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);
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

前两个onPause()onResume()方法是Activity生命周期的一部分,并在Activity暂停/恢复时调用。

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

enter image description here

它与onResume中的其他onPauseActivity方法一起使用的原因是因为您调用了SurfaceView onPauseMySurfaceView()onResumeMySurfaceView() Activity中各方法的方法。如果你没有这样做,你的SurfaceView方法永远不会被调用,因此永远不会停止/启动线程。