I have a task stack of A > B > C. I am currently on C, and then I press the home button. I get a notification with the intent to take me to Activity A. I press the notification, and I'm at A but if I press back, I go to C, then B, then A.
I am setting up my PendingIntent like so. Anything clearly wrong with it?
final Intent notificationIntent = new Intent(mContext, ActivityA.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);
EDIT 1:
I tried the suggestion here: Clear all activities in a task?
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
but I still get the same result. My activity A starts in my application task stack, I press back and go to C, then B, then A again.
I am starting to think that this is either not possible in Android or it's not possible when using a pending intent.
EDIT 2: This is not a matter of what flags are needed. More of an issue of what could be going wrong that the flags seem to have no effect.
My solution code:
A solution in terms of intent flag would be as following:
launchMode:singleTask
for Activity AUse the following code block:
What happens here is that when Activity A is launched using notification, by the definition of
Intent.FLAG_ACTIVITY_NEW_TASK
, since there exists a task A>B>C containing A, this task is brought forward also destroying the activities B and C and A starts with theonNewIntent --> onRestart --> onStart --> onResume
Now the reason, why it doesn't behave nicely with just the intent flags is that, the Activity A wasn't launched with
singleTask
mode and Android somehow doesn't maintain the state of it.if u r using solution 1 then Just remove the
launchMode="singleTask"
from manifest1)
OR
2)
OR
3)