I am facing a strange issue in one of my android applications. My application draws on a surfaceview using a seperate thread. When the application is paused, the rendering thread is stopped. I am handling the shutdown of the thread in the application's onPause method. After resuming to the application a new instance of the rendering thread is recreated in the applications onResume method.
The whole thing works out as it is supposed to be when I pause the application once. However, when I try it the second time, the onPause method of my activity is not called. The rendering thread keeps running in the background, even though the application is not visible anymore.
To pause the activity I am using the home button. As far as I understood the lifecycle, this should trigger onPause, and onStop afterwards. The first time I do this it works perfectly, but I am wondering why it does not work the second time.
Here is some code of my onPause method:
@Override
public void onPause() {
Log.v(null, "calling onpause");
gameview.getHolder().removeCallback(this);
gameview.setFocusable(false);
gameview.setOnTouchListener(null);
if (!isInitializing) {
thread.skipPause = false;
thread.setRunning(false);
boolean retry=true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
thread=null;
} else {
shouldBePaused = true;
}
super.onPause();
}
And here is my onResume method:
@Override
public void onResume() {
gameview.getHolder().addCallback(this);
gameview.setFocusable(true);
gameview.setOnTouchListener(this);
thread = new MainThread(gameview.getHolder(), this);
super.onResume();
}
The question I am wondering about is why onPause is not being called, even though I press the home button. Maybe I recreate the thread in a wrong way? Any help would be appreciated.
I resolved the problem. The problem was that the join-method finished the rendering thread before it released the canvas of the surfaceview.
I assume that this way the canvas stayed locked even after the disappearing of the activity. Possibly, for this reason the onStop method was not called.