1

If I launch the app from the home screen (by taping the app icon), then I navigate through the app and then I leave the app, when I reopen the app the same way (by taping the app icon from the home screen) it will resume the previous state and show the last activity I was on before leaving the app. That part works as expected.

Here is the issue:

If I first launch the app from the play store or manually from the apk installer, and then reopen the app another way (by taping the app icon from the home screen for instance), the app will start a new instance of the main activity and will add it the previous navigation stack (If I press the back button it will go back to the last activity I was on before leaving the app).

I always want the app to open the last activity from background, no matter how the app was first launched (via the home screen or the play store or manually). I already tried stuff like that in the main activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!isTaskRoot()
        && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
        && getIntent().getAction() != null
        && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

        finish();
        return;
    }
    [...]
}

but the finish() call crashes the app.

AW5
  • 406
  • 3
  • 12

1 Answers1

0

I actually found why finish() crashed the app: onDestroy() was called and it tried to unregister a receiver that was not registered yet, so the app crashed: Unable to destroy activity [...] Receiver not registered.

So that code actually works fine for my issue:

if (!isTaskRoot()
        && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
        && getIntent().getAction() != null
        && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

    finish();
    return;
}

I just didn't pay attention to onDestroy()

AW5
  • 406
  • 3
  • 12
  • Why did you need to call `finish()` in the first place? – IgorGanapolsky Oct 07 '20 at 14:28
  • To remove the duplicate instance of the main activity that was added in the existing navigation stack, in order to go back to last opened activity by the user when he left the app. I'm calling `finish()` only if that weird situation happens where the app restarts from the main activity and adds it to an existing navigation stack instead of presenting the last activity from that navigation stack. – AW5 Oct 08 '20 at 01:46
  • Android offer `Intent` flags for launching and keeping activities. You can declare them in **AndroidManifest.xml** – IgorGanapolsky Oct 08 '20 at 13:17
  • I know that, that didn't fix my issue, as I explained in my question, it's an edge case that only happens under certain circumstances. – AW5 Oct 08 '20 at 17:06
  • 1
    You can safeguard unregistering a `receiver` with a **try / catch** statement. – IgorGanapolsky Oct 08 '20 at 18:06
  • Thanks for your help but that's what I did already, as you can see, the question is marked as solved already. – AW5 Oct 08 '20 at 22:06