About the why: technically this is clear and straight forward. From a users perspective view, it might be a difference because if he opens an app from the "app history" he actually wants to remain in the last state he left the app from. If he taps on the app icon, he stupidly says us "open the app" not expected to resume any "session"
I need the same.
If system don't kill the application's process, all works fine (as user expects): system switches to last shown window and paints it as expected.
But if system kills the application's process (due to low memory for example), process is visible in history anyway.
For detection of "necessity of state restoring" I use value of parameter savedInstanceState in function
@Override
protected final void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
boolean needRestoreState = savedInstanceState != null;
....
}
When user starts app at first time, savedInstanceState is null, and needRestoreState == false; Else, when user switches to application in recent apps list, savedInstanceState != null, and needRestoreState == true. All fine, we start restoring.
At lot of Android devices all works fine, until I tried to use Android 9.0 device. At Android 9.0 savedInstanceState ALWAYS IS NULL (((. But exactly same application works fine at Android 7.0!
I found the solution. I use Activity's flag FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY. It set to 1, if the application launch from history. It works even at Android 9.0:
protected final void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
boolean from_history = GlobalSettings.getFlagValue(this.getIntent(), Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
bool needRestoreState = savedInstanceState != null || from_history;
...
}
private static boolean getFlagValue(Intent intent, int flagMask)
{
int flags = intent.getFlags();
return (flags & flagMask) != 0;
}