tags:

views:

41

answers:

1

I need to save some state when the user leaves my game during game play. Saving the state doesn't seem to be an issue, but I can't figure out how to restore it. The onCreate function isn't called when the Activity is resumed (only when it is first created), so I can't get my state back there. Logically, I would use onRestoreInstanceState, but it isn't being called.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.d("RocketLaunch", "onRestoreInstanceState()");
    savedState = savedInstanceState;
}

I see log output from onSaveInstanceState, onResume, onCreate, etc, but I never see a log message from onRestoreInstanceState and savedState is always null. Why isn't onRestoreInstanceState being called, and is there another way to get my state back?

Thanks!

A: 

If the user leaves, I believe you need to take care of this in onPause and onResume. In my very limited testing over the last 30 seconds, I found that my onRestoreInstanceState was only called during an orientation change. (And possibly other similar actions)

Or perhaps in onPostCreate, which does give you the saved instanceState bundle.

BenTobin
onPostCreate is also not called when the Activity is resumed... How would I save and restore state in onPause and onResume, since neither one gets access the the savedInstanceState Bundle?
Computerish
Hmm. I see onPostCreate called if I exit and re-enter my app, but not if my app calls another Activity, and I hit back to return to the original.However, my Activity seems to still be in the state I left it, so I don't need to restore anything in this case.Is it possible that you're doing something in one of these methods that is resetting your Activity in some way? onRestartonStartonResumeonPostResume
BenTobin
Sorry for the late response. How are you testing re entering the application? When I enter my application, hit the home key, and then open my application again `onPostCreate` is not being called.
Computerish