I am using a SurfaceView and a rendering thread to develop a game based on structure like LunarLander.
However, I faced many problems, and here I want to point them
I hope it can help
@Override
public void surfaceCreated(SurfaceHolder holder) {
//if it is the first time the thread starts
if(thread.getState() == Thread.State.NEW){
thread.setRunning(true);//riga originale
thread.start();//riga originale
}
//after a pause it starts the thread again
else
if (thread.getState() == Thread.State.TERMINATED){
thread = new MainThread(getHolder(), this);
thread.setRunning(true);
thread.start(); // Start a new thread
}
}
and this
@Override
protected void onPause() {
Log.d(TAG, "Pausing...");
MainThread.running=false;//this is the value for stop the loop in the run()
super.onPause();
}
I've been dealing with the same issue. I'm learning about game development on Android and also based my first project on the Lunar Lander example. I found that the SpriteMethodTest project created by Chris Pruett implements a much better approach in terms of thread management. It's all about using notify and wait methods, though. I couldn't tell whether it's better than your solution or not.
the solution is here
public void surfaceCreated(SurfaceHolder holder){
if (plot_thread.getState() == Thread.State.TERMINATED) {
plot_thread = new WaveformPlotThread(getHolder(), this);
plot_thread.setRunning(true);
plot_thread.start();
}
else {
plot_thread.setRunning(true);
plot_thread.start();
}
}