0

relating to How to detect when an Android app goes to the background and come back to the foreground there are good hints and solutions about detecting resuming user actions.

Unfortunately this doesn't answer a concrete thing to me:
Q: How to detect if user resumed the app by using the launcher link or by using the activity history

Actually it seems to me, the app enters - and system calls - the same callbacks during lifecycle anyway how an app gets reopened.

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"

childno͡.de
  • 4,679
  • 4
  • 31
  • 57
  • 1
    Seriously why did you need it? , It doesn't matter at all. In both, the scenario sample thing happens whether it was from a multitasking window or app launcher. Either way, same sys call will happen. – Malavan Oct 21 '19 at 13:18
  • This smells like a [X/Y problem](http://xyproblem.info/). There's no good reason for wanting to know this. – Zun Oct 21 '19 at 13:31
  • thanks for your feedback @Zun and Malavan-rockzz . I added a phrase, perhaps it is now more likely that you don't bash me ;) – childno͡.de Oct 21 '19 at 13:40

2 Answers2

0

No it's not possible.

By design, the lifecycle is the same for both cases.

childno͡.de
  • 4,679
  • 4
  • 31
  • 57
0

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;
}