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:
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 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.