Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Launch mode 'single instance'

I was going through the documentation for single instance and was trying out few samples. In one of my sample I have three activities: A->B->C , where B has android:launchMode="singleInstance" in manifest. Activity A and C has default launch mode.

Scenario:

After navigating to C via A and B (i.e A->B->C), back button press from Activity C takes to Activity A (C->A), but back button press from Activity A does not quit the app, rather brings the Activity B to front, then back button press from Activity B quits the app.

Question

Why does Activity B comes to foreground when back button is pressed from Activity A?

Other scenario:

Similarliy, from Activity C if user presses device Home button, and come back the app by long home press, C stays in foreground. But back button press flow takes C-> A -> quits the app. This time Activity B does not come to foreground.

like image 770
Pravy Avatar asked Jun 27 '13 07:06

Pravy


People also ask

What is single instance launch mode Android?

Single Top If an instance of the activity already exists at the top of the current task in this launch mode, no new instance will be generated, and the Android system will send the intent data through onNewIntent (). If an instance does not exist on top of the task, a new instance will be generated.

What is launch mode single task?

It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task." This means that when you click the home button all the activities above the single-task activity (which in your case is ActivityB) are removed from the stack.

What is launch single top?

singleTopIf an instance is not present on top of task then new instance will be created. Using this launch mode you can create multiple instance of the same activity in the same task or in different tasks only if the same instance does not already exist at the top of stack.


2 Answers

After navigating from A->B you have 2 tasks: The first one contains A, the second one contains B. B is on top and A is below that.

Now when navigating from B->C Android launches activity C into the task containing A (it cannot launch it into the task containing B because B is defined as "singleInstance", so it launches it into a task that has the same "taskAffinity", in this case the task containing A). To do that, Android brings the task containing A to the front. Now you have 2 tasks: The task containing A and C in the front, and the second one containing B below that.

Now you press the BACK key, which finishes activity C and returns to the activity below that in the task, namely C. You still have 2 tasks: The one containing C in the front, and the one containing B below that.

Now you press the BACK key again. This finishes activity A (and thereby finishes the task that held A) and brings the previous task in the task stack to the front, namely the task containing B. You now have 1 task: the task containing B.

In your other scenario, after navigating from A->B->C, you start with 2 tasks: The task containing A and C in the front, and the second one containing B below that.

Now you press the HOME button. You now say that you "come back to the app by long press". This isn't exactly correct. You can't "come back to the app". You can only "return to a task". But you've got 2 different tasks: If you do a long press you should see the 2 tasks. They probably have the same icon (unless you've provided a different icon for activity B) so you may not be able to tell them apart. If you select the task that contains A and C, then that task will be brought to the front with activity C on top. If you now press the BACK key, activity C will finish and the activity under it, activity A will be shown. If you now press the BACK key again, activity A will be finished and you will be returned to the HOME screen. The task containing B is still in the list of recent tasks, but it is no longer in the task stack under your other task because when you press the HOME button it is like going back to zero. You start all over again. You have no active tasks, so all tasks are in a row, they aren't in a stack and there is no hierarchy.

Also, in your question you use the phrase "quits the app". This also isn't quite correct. The only thing that a BACK button press does is to finish the current activity. If the current activity is the only activity in a task, it also finishes that task. However, it doesn't "quit the app". Especially in your case, since your "app" actually exists in 2 separate tasks.

Hopefully this is clear.

like image 84
David Wasser Avatar answered Sep 25 '22 08:09

David Wasser


From the doc

  1. "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

  2. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task

like image 28
stinepike Avatar answered Sep 22 '22 08:09

stinepike