1

My app has two activities: A and B. A - is the main activity (default for launch), it has action android.intent.action.MAIN and category android.intent.category.LAUNCHER, and A activity has overridden lanchMode="singleTop", it means that if we trying to launch activity A and A is not the top of the task, then OS will create a new instance of A and put it on top.

Steps:

  • launch activity A from apps menu (click on the app icon)
  • click the button in activity A screen to launch B (now activity stack looks like A -> B)
  • press the home button to see the apps menu again and to minimize the app
  • click on the app icon again to launch my app

Result: opened activity B (stack looks like A -> B)

So my question is why OS do not create a new instance of A if my app in the background with task stack looks like A -> B (B places on top, A and B not finished, they in onStop state) and just open the current stack when I tap on app icon from apps menu (that tap send the intent to my app with Launcher intent, and Launcher is described in activity A which has launch mode singleTop)

I think it suppose to open new instance of A (with stack A -> B -> A) because of A has lanchMode="singleTop". Seems like if the app had activities in the background (in onStop state) and it was opened with the same intent as the first time, then Android OS just show the current app task, but I can not find any proof of that.

Alexey Nikitin
  • 604
  • 7
  • 22
  • Yeah that is the right behavior, read the life cycle of an Activity here: https://developer.android.com/guide/components/activities/activity-lifecycle – Shailendra Madda Nov 09 '20 at 06:13
  • @ShailendraMadda Thanks! But could you, please, provide a quote with the proof of this behavior? – Alexey Nikitin Nov 09 '20 at 06:25
  • When you move from Activity - A to Activity - B then Activity A is going to back stack. Then if you click the Home button then your Activity-B is still there right? If you finish the activity then only it will launch Activity-A otherwise it opens the last Activity which is B in your case. – Shailendra Madda Nov 09 '20 at 06:29
  • @ShailendraMadda Yes, I found it empirically, but I need some proof of that behavior. Could you, please, provide a quote from documentation? – Alexey Nikitin Nov 09 '20 at 06:39
  • Check it here: https://developer.android.com/guide/components/activities/activity-lifecycle#onpause – Shailendra Madda Nov 09 '20 at 06:55
  • @ShailendraMadda Sorry, but I can't find any relative information in the onPause section of activity lifecycle docs – Alexey Nikitin Nov 09 '20 at 07:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224310/discussion-between-shailendra-madda-and-alexey-nikitin). – Shailendra Madda Nov 09 '20 at 07:22

1 Answers1

1

This behaviour actually has nothing to do with the launch mode of the Activity. Actually, in this case, Android isn't launching any Activity. You would see this if you added logging or set a breakpoint at onNewIntent() which would be called in the case where Android wanted to launch the Activity, saw that there was already an instance on top of the stack in the task, and routed the new Intent to the current instance by calling onNewIntent().

What is haooening here, when the user taps the app icon on the HOME screen, is that, before launching any Activity, Android looks to see if there is already a task in the background that was started with this same Intent (in this case, ACTION=MAIN, CATEGORY=LAUNCHER, COMPONENT=ActivityA). If it finds a task that was started with the same Intent, it simply brings that task to the foreground in whatever state it was in (exactly as it was when it was moved to the background) and that's it. It doesn't launch any new Activity, it does not call onNewIntent() on the topmost Activity.

This is documented here (although it is easy to miss): where it says:

The device Home screen is the starting place for most tasks. When the user touches an icon in the app launcher (or a shortcut on the Home screen), that app's task comes to the foreground. If no task exists for the app (the app has not been used recently), then a new task is created and the "main" activity for that app opens as the root activity in the stack.

David Wasser
  • 93,459
  • 16
  • 209
  • 274