I have a problem managing Android tasks and intentions.
Scenario
- The user receives a push with a deep connection in the application.
- We show a notification that puts the URI in the Intent data.
- The user clicks the notification and is transferred to the application and redirected to some Feature1Activity described by a deep link.
- The user looks around and returns from the application.
- Later, the user opens the application from the history (long press button on the house or multitasking).
- Now, to launch the application, the same intention is used that was used in the notification (with a deep link in Intent Data).
- Therefore, the user is re-entered into Feature1Activity.
Problem:
Starting an application from history (a button with a long press on the house or multitasking) does not reset the Task (which it does when it starts from the application icon).
I understand that starting an application from history should not reset the task, since it is intended to be used as "get-right-back-where-you-were". However, in my case, this is a problem, since starting the application from the notification is a one-time thing.
Has anyone else encountered this problem? Does anyone know a solution?
More details>
The target inside the PendingIntent is constructed as follows:
Intent intent = new Intent (Intent.ActionView); intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); intent.setData (Uri.Parse (DEEP_LINK_URL));
This day I learned about FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET and really thought that it would save my problem from git, but that does not make any difference.
Three types of activity are interesting:
SplashActivity (main launcher & listener of the deep-linking schema -- this activity just redirects either to login or OverviewActivity) OverviewActivity (authorized user main activity) Feature1Activity (any feature that the deep-link is pointing to)
What happens when the user clicks the notification is that SplashActivity acts as a listener for the schema and converts the deep link URL into two intentions to launch OverviewActivity and Feature1Activity using Activity.startActivities (Intent []).
When I look at the intent of the notification inside SplashActivity, it always contains a deep link in the Data.
One work around
Works around by setting some booleanExtra field to the notification intent (for example, "ignoreWhenLaunchedFromHistory" = true), and then check SplashActivity before redirecting
boolean fromHistory = (getIntent().getFlags() & FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY; if (fromHistory && getIntent().getBooleanExtra ("ignoreWhenLaunchedFromHistory", false)) // Don't follow deep-link even if it exists else // Follow deep-link
Except it is hacked and ugly, can you see any problems with this job?
EDIT: The work around only works when I am responsible for sending the intent via a deep link. Because no external source can know about "ignoreWhenLaunchedFromHistory" additionally.