20

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.

2 Answers 2

40

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 A. You still have 2 tasks: The one containing A 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.

7
  • 1
    Thank you so much David..Your answer explains the concept very clearly, really helpful :) Just to add more detail, I had all these three activities A, B and C within same application. On long home press, I could not see two separate tasks, but only one. Also with respect to the OTHER SCENARIO, " If you now press the BACK key again, activity A will be finished and you will be returned to the HOME screen. " Q: why does it not take us task containing B, because task containing B should have been below Task containing A and C right? Please correct me if I am wrong.
    – Pravy
    Jun 28, 2013 at 5:09
  • Please post your manifest, then I can help you more. Just add it to your question as an edit. Jun 28, 2013 at 8:54
  • Hey Pravy, Did your confusion solved at last? why does it not take us task containing B? I have the same confusion. If you know, please tell me. Thank you.
    – CodeAlien
    Dec 15, 2014 at 15:52
  • @CodeAlien I thought my explanation was pretty clear. If you are still confused, you should open another question. Dec 15, 2014 at 16:07
  • @DavidWasser sorry for disturbing you in the comment. I encountered a problem like this: after using installer of android system installed my app, then I open my app directly from installer. It works well. But if I clicked the launcher of app to reenter it, problem happened. It's different from installing the app from android studio or eclipse. It start from the first activity every time. Why this happened? If I killed the app, and just clicked the icon to enter in not from installer. It works well too. It really confused me. Thank you for your help.
    – CodeAlien
    Dec 15, 2014 at 17:24
3

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

3
  • :Thanks for the reply,your first point makes it clear why it come to foreground again. I need one more clarification regarding how the activity stack,various tasks and individual activities are related. AS per my understanding from your reply,the OS maintains a stack called 'activity stack' which contains stack of tasks, which inturn(i.e individual tasks) will contains activities.If any activity is created as single instance, then it will be created as separate task,and that task will be placed at the bottom of the stack. kindly,let me know if my understanding is correct.
    – Pravy
    Jun 27, 2013 at 8:24
  • @Pravy .. your understanding matches with mine :) .. .. if a activity starts normally ( not singleinstance or singletask) then it is just pushed to the stack
    – stinepike
    Jun 27, 2013 at 9:34
  • Well thanks.. but the same behaves differently when user had pressed device home button from activity C. I have updated it in the other scenario. May I know what could be the reason?
    – Pravy
    Jun 27, 2013 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.