I've already been looking for solutions about my problem for much time, but The Android documentation is not so clear and web solutions don't work completely.
I'm developing a game in which the main Activity (where the game takes place) can call another simple Activity and than return back. It would work like this:
- If the user clicks the Home button and then returns to the game, the activity must be stopped and then resumed without pass the onDestroy (otherwise it would lose the game state)
- The same function when the main Activity calls the other Activity and then returns to the game.
- When the user presses the exit button the game must exit at all
For the first two points I tried this to be sure that one and only one main Activity is created and called: android:launchMode="singleTask"
or android:launchMode="singleTop"
but unluckily the first works for the first problem and the second for the second one, not for both!
I think the problem is: with singleTop the main Activity isn't actually on the top of the stack when the second activity calls it (to be clear, I don't understand what should be the situation in which I call an activity that is already on the top of the stack!). Conversely, the singleTask ensures that the Activity is unique in its stack, but after the users click the Home button and returns to the Application the other Task is called, am I right?
Anyway, is this a smart solution or I should save the state of my game in the onStop/onPause and then resume that in the onResume?
For the third point I tried to add the android:noHistory="true"
to every Activity, then when the user clicks the exit button I call a new Activity which do nothing but the finish()
method on the onCreate, this Activity is called with intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
and its launchMode is singleInstance
, but it doesn't work, it simply returns always in the main Activity.