Android - Clear task flag not working for PendingIntent

后端 未结 9 1954
夕颜
夕颜 2020-12-17 09:34

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 notificat

相关标签:
9条回答
  • 2020-12-17 10:20

    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.

    0 讨论(0)
  • 2020-12-17 10: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();
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:24

    The problem you are encountering has to do with Android creating a new task using your PendingIntent and the required Intent.FLAG_ACTIVITY_NEW_TASK. In other words, the flags you are wanting to use have to do with starting activities within the context of a given task stack. A PendingIntent usually does not have a context, and therefore what you are seeing is a task being created for your Activity A pending intent.

    Here's the reference to the docs:

    Android PendingIntent

    The requirement for the first in a series of PendingIntents to have Intent.FLAG_ACTIVITY_NEW_TASK is the issue. Note that a series of pending intents can have the same context as the initial pending intent using the "getActivities" method.

    You are correct that it is not possible to do what you want only using flags. Your code is correct, and to get the behavior that you want you will need too look beyond intent flags.

    There are many other ways, such as using the manifest, running a service, using static fields, etc. to try to manage what you are interested in doing. Besides that, you could possibly use TaskStackBuilder to solve your problem if you decide to go the route of using the Manifest. There are also many other solutions not involving flags, but you specifically asked about using flags.

    0 讨论(0)
提交回复
热议问题