Android - onPause and onResume

2.2k views Asked by At

I have code which randomly draws red circles. It didn't work until I had pause and resume methods in BOTH classes. Without the pause and resume methods, the screen would just be black and not change. Why did I need an onPause and onResume method and why in both classes?

The commented code is all the pause/resume methods.

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

There are 1 answers

4
the-ginger-geek On BEST ANSWER

The first two onPause() and onResume() methods are part of the Activity life cycle and are invoked when the Activity is paused/resumed.

This image illustrates the Android Activity life cycle. You can read up on it HERE

enter image description here

The reason it works with the additional onResume and onPause methods in your Activity is because you invoke your SurfaceView onPauseMySurfaceView() and onResumeMySurfaceView() methods from the respective methods in the Activity. If you didn't do that, your SurfaceView methods would never have been called and thus never stopped/started the thread.