0

This is the behavior I'm looking for:

Start the App and navigating => Activity A > B > C Then I press Home button and open the App again => Go back to C (with stack A B C)

Now I have a service running who creates notifications. Click on the notification => Clear whatever is in the stack and open A then X from onNewIntent() (stack is now A > X)

But the current behavior when I click on a notification is A B C A X (X on top)

As I understand, this is happening because of the wrong context. See the following post for more information about the blank context issue https://stackoverflow.com/a/24999724

So my conclusion is to use the "standard" launchMode and find a way to clear the stack from all activites when clicking on notifications. Is there any patterns to implement in order to get this behavior ?

Community
  • 1
  • 1

1 Answers1

0

This has nothing to do with "wrong context". If you want clicking on a Notification to clear the task stack and start a new instance of ActivityA, then use the following flags in the Intent you put in the Notification:

Intent.FLAG_ACTIVIY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP

Assuming that ActivityA is still active (not finished) and at the root of the task stack, his will clear the task stack back to ActivityA (including clearing the existing instance of ActivityA), then create a new instance of ActivityA and call onCreate(), etc.

If you want to reuse the existing instance of ActivityA, you can use this instead:

Intent.FLAG_ACTIVIY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP

Assuming that ActivityA is still active (not finished) and at the root of the task stack, his will clear the task stack back to ActivityA (without clearing the existing instance of ActivityA), then call onNewIntent() on the existing instance of ActivityA.

David Wasser
  • 93,459
  • 16
  • 209
  • 274