Emmmm, well, hello, it's me again, well I've been researching all over the internet how to solve this problem and until now I do not know how to solve it, the question is simple. How to wait for a thread to be processed in order to destroy the Activity?
The case is the following, I have a SurfaceView, which is connected to a Thread since since the SurfaceView is created it starts the thread and starts to draw everything on the screen, the point is that it is a game, and as you know thread can not be stopped until the game is closed as the "Character" moves without stopping, the problem is as follows, when pressing the "Back" or "Home" button it automatically destroys the SurfaceView, but the Thread continues to draw, and this causes the application to lock and a message saying:
Unfortunately ... You have stopped.
Then what I want to do is before the SurfaceView is destroyed, I want it to stop the Thread, but to stop it, it must wait until it finishes drawing (that is, it finishes turning over (while) and can just put it in false and so stop circling and finish the process.).
I already tried to do this:
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
GameLoop.setRunning(false);
}
Or something more advanced like this:
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
boolean retry = true;
GameLoop.setRunning(false);
while (retry) {
try {
GameLoop.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
And something more advanced, which is to play with the cycle of life, before moving to the mode "onStop ();" than in the onPause () mode; wait until the thread finishes turning to be able to just pause it, and then go to the onStop mode and then on Destroye, but this happens milliseconds that do not wait for the thread to finish and if the condition of the GameLoop is fulfilled set to false, but the SurfaceView continues to draw, and this causes the application to crashee, I would just like to know how to do that in the onPause () method; wait for about 2 seconds and then the application goes to the onStop () mode;
I can not pass them the code because they are like 1500 lines, I hope you can help me, thanks.