Hopefully someone can help me figure out, if not a solution, at least an explanation for a behaviour.
On some devices, pressing the launcher icon results in the current task being resumed, on others it results in the initial launch intent being fired (effectively restarting the app). Why does this happen?
When you press the "Launcher Icon" the app starts normally - That is, I assume, an Intent is launched with the name of your first
Aha! (tldr; See the statements in bold at the bottom)
I've found the problem... I think.
So, I'll start off with a supposition. When you press the launcher, it either starts the default
Activity or, if a
Task started by a previous launch is open, it brings it to the front. Put another way - If at any stage in your navigation you create a new
finish the old one, the launcher will now no longer resume your app.
If that supposition is true, I'm pretty sure that should be a bug, given that each
Task is in the same process and is just as valid a resume candidate as the first one created?
My problem then, was fixed by removing these flags from a couple of
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
While it's quite obvious the
FLAG_ACTIVITY_NEW_TASK creates a new
Task, I didn't appreciate that the above supposition was in effect. I did consider this a culprit and removed it to test and I was still having a problem so I dismissed it. However, I still had the below conditions:
My splash screen was starting the "main"
Activity in my app using the above flag. Afterall, If I had "restart" my app and the
Activity was still running, I would much rather preserve it's state information.
You'll notice in the documentation it makes no mention of starting a new
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
So, I had the situation as described below:
Bwishes to restart a service so sends the user to
Awhich has the service restart logic and UI (No flags).
At this stage the second
FLAG_ACTIVITY_CLEAR_TOP flag is restarting
B which is in the task stack. I'm assuming this must destroy the
Task and start a new one, causing my problem, which is a very difficult situation to spot if you ask me!
So, if all of my supposition are correct:
Launcheronly resumes the initially created Task
FLAG_ACTIVITY_CLEAR_TOPwill, if it restarts the only remaining
Activity, also recreate a new