0

i'm a android beginner and programming currently a android app with the following rough frame that you find often in tutorials for a game loop.

The app has one Activity class, a view class and a thread class for the game loop.

The thread will be started if the surface will be created with thread.start()

Everything is running fine but my problem is now if the app is going in the background (is not visible anymore) and will be later activ again. The app crashed. The debugger said me "Thread already started"

let me show you in short the structure

The Activity class:

public class TestActivity extends Activity{

...

  protected void onCreate(){
...

  test = newTestView(this);
  setContentView(test)
...

  }
...
}

The Thread class:

public class Test extends Thread{

...

public void setrunning(boolean run)
{
    isrunning=run;
}

public void run(){

...
while (isRunning) {
...
        theView.onDraw(theCanvas);
...

}

The View class:

public class TestView extends SurfaceView{

...

public void surfaceDestroyed(SurfaceHolder holder) {

    theGameLoopThread.setRunning(false);
    theGameLoopThread.join();

}

public void surfaceCreated(SurfaceHolder holder) {

    theGameLoopThread.setRunning(true);
    theGameLoopThread.start();

}

...

}

I think the threat wasn't finished and still exist in status supended and can't restarted if the surface will be created again and the app crashed. If i use a if loop with

if(theGameLoopThread.getState()==Thread.State.NEW)
                {theGameLoopThread.start();}

to avoid a start if thread is not new then the screen is only black, because the onDraw methode will not run.

How can i restart the threat or kill and start again?

I tested Thread.join(), Thread.resume() and so son.

Have some one an idea or can help to correct my app structure?

Thanks Speisezwiebel

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

i think making a new instance of your Thread in else block of the:

if(theGameLoopThread.getState()==Thread.State.NEW)
            {theGameLoopThread.start();}

will fix your issue.

  • ah, yes good idea, i will try that. But i will be sure that the old thread will be destroyed. Do you know how i can do that or will the old threat do that alone? – speisezwiebel Sep 27 '18 at 10:52
  • i don't think there would be a good way to kill a thread , but you can read this :https://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java – Mohsen Einhesari Sep 27 '18 at 10:57
  • i tried your suggestion and it works fine for me. I tested this solution on different devices and only on an LG mobile i had a bad side effect. The restored app has distorted presentation (font is too big and looks like that the used canvas looks bigger as the real mobile screen so that some content is outside of the screen). Do you have an idea how i can reset the surface and rebuild complete new? (I check in the app the screen size and scale the presentation on every device automatically with a determined factor) – speisezwiebel Sep 28 '18 at 08:28
  • i didn't really get the issue but can you override Onresume in your Main activity and rebuild surface every time app comes back from background? – Mohsen Einhesari Sep 29 '18 at 12:08