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