Set the flag to onSaveInstanceState () to determine the type of output in onDestroy ()

For online games, it would be great to know if only Android Activity onDestroy () is called, because Android is going to recreate it (for example, rotating the device) or if the user decided to exit the game.

My plan was to set the flag in Activity onSaveInstanceState() when Android is likely to recreate the Activity :

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

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

  • If the flag is set (true), do not miss the user from the online game.
  • If the flag is not set (false), release the user from the online game, as he voluntarily left the game.

Is this the right approach? And if so, is this recommended or is there a better solution? I hope because I do not really like this solution ...

+8
android android-activity
source share
6 answers

I suggest you remove this game logic from the activity life cycle. Create a service. If no one is attached, all actions are dead. Someone is attached - keep working.

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

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

+5
source share

You can simply avoid restarting during rotation by processing these configuration changes by code. You can do this in your Manifest.xml as follows:

 <activity android:name=".MainActivity" android:configChanges="orientation|screenSize|keyboard|keyboardHidden" android:label="@string/app_name" > 

So, your application will not restart when spinning and if the keyboard is open / closed.

I think this solution is much simpler.

In this case, you almost do not need to contact onSaveInstanceState() to exit, except that you run another intent / action in which you need to save the state of the game. Please note that a phone call also interrupts your code. I know several games with funny bugs where time is reset, but not an estimate.

+3
source share

I would just simplify all this and set a flag that switches when the user leaves the game, something like:

 void exitGame() { mUserExited = true; finish(); } 

(Or you may need more logic if you need to destroy multiple actions)

Then check the flag in onDestroy ().

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

In addition, remember that the default behavior of the back button is to end () the current activity (if nothing else is above it) - this will not be considered an β€œexit” in this case. The behavior here is up to you.

+2
source share

Activity has an isFinishing () method, which you are probably looking for.

See: stack overflow.squite

+2
source share

If you need to know this, you should think about how to handle the rotation and other changes associated with a configuration change yourself, rather than letting the system do this. If you set in your manifest that the operation processes configChanges, it will call onConfigChange when it is spinning, and not destroying and recreating the activity. A large number of applications do this, destroying and recreating everything that happens in Android, absolutely braked.

+1
source share

onRestoreInstanceState () will be called if, when restoring / re-creating, if the activity, if it was killed by the android, saves the state of the activity user interface and some values ​​that you can override onSaveInstanceState but since the onSaveInstanceState () function cannot be called, you should use it only for recording transient state (user interface state) - you should not use it to store persistent data. Instead, you should use onPause () to store persistent data (such as data that should be stored in the database) when the user leaves this activity. OnRestart is also called after onStop () when the current activity is re-displayed to the user. Therefore, perhaps you can save your state in onPause / if onRestart is called, it looks like it is displayed again, and if onCreate is called without onRestart, it is recreated. Another solution is to use the singleInstance and override onNewIntent method, which is called if the activity is not destroyed, but is restarted with a new intention.

0
source share

All Articles