Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ...

like image 450
caw Avatar asked Jan 21 '13 05:01

caw


3 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 ).

like image 91
Leonidos Avatar answered Oct 16 '22 10:10

Leonidos


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

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize|keyboard|keyboardHidden"
    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.

like image 3
rekire Avatar answered Oct 16 '22 10:10

rekire


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;
   finish();
}

(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.

like image 2
SirKnigget Avatar answered Oct 16 '22 10:10

SirKnigget