java – Wait for the process of a Thread to finish in order to destroy the activity

Question:

Emmmm, well, hi, it's me again, well I've been researching all over the internet how to solve this problem and until now I don't know how to solve it, the question is simple. How to wait for a thread to finish processing 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, the Thread starts and begins to draw everything on the screen, the question is that it is a game, and as you know the thread is not It can stop until the game closes as the "Character" moves without stopping, the problem is the following, when pressing the "Back" or "Home" button it automatically destroys the SurfaceView, but the Thread ) keep drawing, and this causes the application to hang and a message will appear saying:

Unfortunately … Has stopped.

So what I want to do is before the SurfaceView is destroyed I want it to stop the Thread, but to stop it you must wait until it finishes drawing (That is, finish turning (while) and just be able to set it to false and so let go around and finish the process.).

I've 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 life cycle, before going to the "onStop ();" than in onPause () mode; wait for the thread to finish turning to be able to just pause it, and then go to onStop mode and then onDestroye, but this occurs milliseconds that does not wait for the thread to finish and if the condition is met that the GameLoop is set to false, but the SurfaceView keeps drawing, and this causes the application to crash, I just want to know how to do that in the onPause () method; wait about 2 seconds and just the application goes to onStop () mode;

I cannot pass the code to you because there are like 1500 lines, I hope you can help me, thank you.

Answer:

There are several ways to detect the termination of a Thread, one is by detecting the termination of your Thread instance, through the TERMINATED state:

if(myThread.getState()!=Thread.State.TERMINATED){
//termina la actividad.
    finish();
 }

Or through an interface that is triggered at the end of the Thread, even through an AsyncTask.

Your main problem is that you want to close the activity when there is a Thread that is modifying the UI, I propose as a solution to call the interrupt() method of your Thread instance to force the Thread to stop:

myThread.interrupt ()

As a complement to the answer, as for this you want:

onPause (); wait about 2 seconds and the application just goes to onStop () mode;

Wait, it cannot be done since you would be modifying the natural life cycle of the Activity.

Scroll to Top