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.
This behaviour actually has nothing to do with the launch mode of the
Activity. Actually, in this case, Android isn't launching anyActivity. You would see this if you added logging or set a breakpoint atonNewIntent()which would be called in the case where Android wanted to launch theActivity, saw that there was already an instance on top of the stack in the task, and routed the newIntentto the current instance by callingonNewIntent().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 sameIntent(in this case, ACTION=MAIN, CATEGORY=LAUNCHER, COMPONENT=ActivityA). If it finds a task that was started with the sameIntent, 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 newActivity, it does not callonNewIntent()on the topmostActivity.This is documented here (although it is easy to miss): where it says: