Document Explanation for PendingIntent.FLAG_CANCEL_CURRENT

From the documentation of the FLAG_CANCEL_CURRENT Intent FLAG_CANCEL_CURRENT on Android:

By canceling the previous pending intent, this ensures that only objects that receive new data can start it. If this assurance is not a problem, consider FLAG_UPDATE_CURRENT

Can someone explain what this line means?

+8
android
source share
1 answer

Once you create a new PendingIntent with FLAG_CANCEL_CURRENT , everything that contains the previous PendingIntent for the same Intent will no longer be able to execute this original PendingIntent .

For example, suppose we have this:

 Intent i=new Intent(this, Foo.class); i.putExtra("key", 1); PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0); 

and we use this PendingIntent with, say, a Notification .

Later we will do:

 Intent i=new Intent(this, Foo.class); i.putExtra("key", 2); PendingIntent pi2=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); 

At this point, the PendingIntent created initially ( pi ) is no longer valid, and everything we use pi2 for will see the updated optional value ( 2 ).

If instead we did:

 Intent i=new Intent(this, Foo.class); i.putExtra("key", 2); PendingIntent pi2=PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

At this point, pi and pi2 both represent the same PendingIntent , and both will see the updated optional value ( 2 ).

Or if we did:

 Intent i=new Intent(this, Foo.class); i.putExtra("key", 2); PendingIntent pi2=PendingIntent.getActivity(this, 0, i, 0); 

At this point, pi and pi2 still represent the same PendingIntent , but the additional parameters are not changed, because getActivity() returns the original PendingIntent without applying new additional functions.

In most cases, FLAG_UPDATE_CURRENT is a great answer when you try to replace additional functions inside a PendingIntent .

+22
source share

All Articles