Android - Clear task flag not working for PendingI

2019-01-26 05:27发布

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.

9条回答
干净又极端
2楼-- · 2019-01-26 06:20

My solution code:

    public class MyApplication extends Application{
    private static int activityCounter;
    public Activity A,B,C;
    public void incrementActivityCounter()
    {
        activityCounter++;
    }
    public void decrementActivityCounter()
    {
        activityCounter--;
        if (activityCounter == 0)
        {
            A.finish();
            B.finish();
            C.finish();
        }
    }
}


public class A extends Activity{
   @Override
    protected void onStart()
    {
        super.onStart();
        application.incrementActivityCounter();
    }

    @Override
    protected void onStop()
    {
        application.decrementActivityCounter();
        super.onStop();
    }
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-26 06:23

A solution in terms of intent flag would be as following:

  1. In Manifest, add launchMode:singleTask for Activity A
  2. Use the following code block:

    final Intent notificationIntent = new Intent(mContext, ActivityA.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);
    

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 the onNewIntent --> onRestart --> onStart --> onResume

If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent()

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-26 06:24

if u r using solution 1 then Just remove the launchMode="singleTask" from manifest

1)

 final Intent notificationIntent = new Intent(mContext, ActivityA.class);

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

 PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0);

OR

2)

       <activity 
       android:name=".ActivityA" 
       android:theme="@style/theme_noActionBar" 
       android:noHistory="true">

OR

3)

<activity android:name=".ActivityA" android:launchMode="singleTop">
查看更多
登录 后发表回答