Home » Android » android – Setting flag in onSaveInstanceState() to determine exit type in onDestroy()

android – Setting flag in onSaveInstanceState() to determine exit type in onDestroy()

Posted by: admin June 15, 2020 Leave a comment


For online games, it would be great to know if an Android Activity‘s onDestroy() is only called because Android is going to re-create it (e.g. device rotation) or if the user opted to exit the game.

My plan was to set a flag in the Activity‘s onSaveInstanceState() when Android is probably re-creating the Activity:

private boolean mDestroyedForReCreation;
protected void onSaveInstanceState() {
    mDestroyedForReCreation = true;

If you did this, you can check mDestroyedForReCreation in onDestroy():

  • If the flag is set (true), don’t dismiss the user from the online game.
  • If the flag is not set (false), dismiss the user from the online game as he did voluntarily exit the game.

Is that a correct approach? And if yes, is it recommended or is there any better solution? I hope so because I don’t really like that solution …

How to&Answers:

I suggest you to remove such kind of game logic from activity’s life cycle. Create a Service. If no one binded – all activities are dead. Is someone binded – keep working.

If you do not want to create service, you can use onRetainNonConfigurationInstance method. Here is example.

You should use onRetainNonConfigurationInstance because it is called by the system, as part of destroying an activity due to a configuration change, when it is known that a new instance will immediately be created for the new configuration. onSaveInstanceState called when android going to kill activity and maybe restore it sometimes or maybe not ).


You can simply avoid restarts on rotation by handling this configuration changes by code. You can do this in your Manifest.xml like this:

    android:label="@string/app_name" >

So your app won’t restart on rotation and if the keyboard opened/closed.

I think this solution is much simpler.

In this case you almost don’t need to handle onSaveInstanceState() for exiting, except you start another intent/activity where you need to save your game state. Note that a phone call will also interrupt your code. I know some games with funny bugs where the time is resetted but not the score.


I would just simplify the whole thing, and set a flag that is toggled when the user exits the game, something like:

void exitGame() {
   mUserExited = true;

(Or you might need more logic if you need to destroy multiple activities)

Then check the flag in onDestroy().

Whatever logic you have about configuration changes (rotation, etc.) will have nothing to do with the exit game flag.

Also, remember that the ‘back’ button’s default behavior is to finish() the current activity (if nothing else is above it) – that won’t count as an “exit” in this case. The behavior here is up to you.


Activity has a method called isFinishing() that is probably what you are looking for.

See: https://stackoverflow.com/a/9621078/445348


If you need to know this, you should consider handling rotation and other configuration changed events yourself rather than letting the system do it. If you set in your manifest that the activity handles configChanges, it will call onConfigChange when it rotates rather than destroying and recreating the activity. A large amount of apps do this, the whol destroying and recreating on rotation thing Android does is absolutely retarded.


onRestoreInstanceState() will be called if when it is restored /recreated , if the activity if killed by android it saves its activity UI state and some values you can override onSaveInstanceState
but because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity. Also onRestart will be called after onStop() when the current activity is being re-displayed to the user. So probably you can save your state in onPause / if onRestart is called it is like it is being re displayed , while if onCreate is called without onRestart it is recreated . Other solution is to use singleInstance and override method onNewIntent which is called if activity is not destructed but like restarted on a new intent .