When we launch Android applications from the history (recent applications), the application can be launched using basically three different Intent flags.
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
This is when the activity is launched from the history of the application, which was minimized (long press the home key).
Constant value: 1048576 (0x00100000)FLAG_ACTIVITY_NEW_TASK
This is when the activity is triggered through the "click on the application icon" or through the " Intent filters ". Here the action will be the beginning of a new challenge in this story stack.
Constant Value: 268435456 (0x10000000)FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY | FLAG_ACTIVITY_NEW_TASK
This is when the application was closed by clicking the "Back" button, and then resuming it from the "History" (recent applications).
Constant value: 269484032 (0x10100000)
A constant value can be obtained using getIntent().getFlags()
In the third case, Android reloads the latest Intent from its memory. This way your application ( getIntent ) will have values from the last intention that the application started.
In fact, the application should behave as if it is a new launch, with the goal of creating values for the new launch, and not the intent values of the previous launch. This behavior can be seen if you launch the application by clicking the application icon, it will never have the old intent values. This is because Android uses the following intent filter for this scenario.
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter>
But in the third case (the application that was closed is launched from the history of recent applications), Android OS uses the last intention, which launched the application before its release (using the "Back" button). This way you get the old intent values, and the application flow is not correct.
Removing an intent is one way to solve it, but it will not solve the problem completely! Because the Android OS restarts the Intent from the last application launch, not the last instance of the launch intent.
The pure way to avoid this is to process it by getting an Intent type to determine the type of startup.
So, in your LaunchActivity (one that has an intent filter defined in the manifest), you can use the following code in the onCreate() , onStart() or onResume() .
if(getIntent().getFlags() == (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)) { //app is launched from recent apps after it was closed normalLaunch(); } else { String intentAction = getIntent().getAction(); String scheme = getIntent().getScheme(); //app is launched via other means // URL intent scheme, Intent action etc if("https".equalsIgnoreCase(scheme)) { // URL intent for browser } else if("com.example.bb".equalsIgnoreCase(intentAction)) { // App launched via package name } else { // App was launched via Click on App Icon, or other means normalLaunch(); } }
I assume normalLaunch() should not use parameters from Intent; otherwise, you will need to separate and optimize the default startup method so that you do not use Intent parameters.