Hi There,
I am basing my game off of the Lunar Lander demo.
The problem I am having is my game not restoring its last saved state
when it comes back into focus.
When the game loses focus/quits it saves all the information to a
Bundle correctly, then runs surfaceDestroyed() to shut down the
thread.
When I restart the game, I thought onCreate(Bundle savedInstanceState)
method was supposed to always start first, then restore the saved
Bundle if it is not "Null". But because it sees that I have started/
created a thread last time, it jumps straight to surfaceCreated
method.
The only thing I can think of is to find a way to get rid/destroy the
thread so that it runs onCreate at the start. However the stop() and
destroy() methods for threads have been depreciated. Is there anyway
to do this? Or is there a better method of saving an instance of the
game?
Basically when the game restarts it goes to surfaceCreated() instead
of onCreate().
Note: In surfaceDestroyed() if I force the application to crash here,
that gives me an "Application has stopped unexpectedly" error which
causes the thread to die. When I restart my game it runs onCreate,
sees that there is a bundle saved and starts from where the game left
off perfectly fine!
****BallDrop.java*****
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.balldrop);
mBallDropView = (BallDropView) findViewById(R.id.BallDrop);
mBallDropThread = mBallDropView.getThread();
mBallDropView.setTextView((TextView) findViewById(R.id.text));
if (savedInstanceState == null) {
mBallDropThread.setState(BallDropThread.STATE_READY);
} else {
Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
if (map != null) {
mBallDropThread.restoreState(map);
} else {
mBallDropThread.setState(BallDropThread.STATE_PAUSE);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//Store the game state
super.onSaveInstanceState(outState);
outState.putBundle(ICICLE_KEY, mBallDropThread.saveState());
}
****BallDropView.java*****
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
// FIX FROM LUNAR LANDER --->
if(thread.getState()== Thread.State.TERMINATED){
thread = new BallDropThread(getHolder(), getContext(),
getHandler());
thread.setRunning(true);
thread.start();
// <--- FIX FROM LUNAR LANDER
}else {
thread.setRunning(true);
thread.start();
}
}
/*
* Callback invoked when the Surface has been destroyed and must
no longer
* be touched. WARNING: after this method returns, the Surface/
Canvas must
* never be touched again!
*/
public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to
finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
/* mBallDrop.finish(); //CAUSES APP TO CRASH ON EXIT
WHICH KILLS THREAD*/
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en