Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: resume an Activity with singleTop or SingleTask

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:

  1. 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)
  2. The same function when the main Activity calls the other Activity and then returns to the game.
  3. 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.

like image 512
mordicchio Avatar asked Oct 22 '12 22:10

mordicchio


People also ask

What is the difference between singleTop and singleTask?

standard and singleTop comes in one side and singleTask and singleInstance comes in another side. The main difference between standard and singleTop is in standard, every time a new intent for standard activity, a new instance is created.

What is difference between singleTask and SingleInstance in Android?

SingleInstance — This is the same as single-task mode but, we cannot launch any other activity inside the task which has an activity launched with single-instance mode.

What is meant by singleTop in activity instance?

2. singleTop. In this launch mode if an instance of activity already exists at the top of the current task, a new instance will not be created and Android system will route the intent information through onNewIntent(). If an instance is not present on top of task then new instance will be created.

How do I use singleTask on Android?

Single Task In this method of operation, a new task is always generated, and a new instance is added to the task as the root one. If the activity already exists on another task, no new instance is created, and the Android system transmits the intent information via the onNewIntent() function.


1 Answers

You definitely don't want to mess with the launchMode. That isn't the way to solve this problem and will just cause you more problems.

Android can (and will) kill your process whenever it wants to (but especially if your application is in the background). Because of this, you need to save the state of your game in onPause(), because that is the only opportunity that you are guaranteed to have.

Anyway, if you save the game in onPause() the standard behaviour will cover your first 2 points.

For your 3rd point, there are a few options:

  1. You can start all activities with startActivityForResult() see my answer here
  2. When you want to "exit", you can just go back to your root activity (the one that starts your application) and tell it that you want to exit.

Like this:

Intent intent = new Intent(this, MyRootActivity.class); // this is the starting activity for your application
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // tells Android to finish all other activities in the stack
intent.addExtra("exit", "true"); // this tells your root activity that you want to exit
startActivity(intent);

Now, in your MyRootActivity you want to add this code to onCreate():

Intent intent = getIntent();
if (intent.hasExtra("exit")) {
    // User wants to exit
    finish();
}
like image 167
David Wasser Avatar answered Nov 15 '22 05:11

David Wasser