6

when we pass 0 as flag to PendingIntent as below :

PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);

does it follow any flags rules means does 0 correspond to any flag by default.

If we create another PendingIntent as

 PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);

will it be same as earlier and if i make any change to the data in Intent , will it now correspond to new data in Intent or still have old data.

Another problem i am facing in this is I am trying to check flag

PendingIntent.FLAG_NO_CREATE

I have written the following code :

Intent i=new Intent(this,NotifResult.class);

        i.putExtra("number",50);
        PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
NotificationCompat.Builder nb=new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.ic_launcher);
        nb.setTicker("ticker is here");
        nb.setWhen(System.currentTimeMillis())
        .setContentTitle("just the title")
        .setContentText("and the description")
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentIntent(pi);


Notification notif=nb.build();
        NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(11, notif);

        i.putExtra("number",80);

        PendingIntent pisecond=PendingIntent.getActivity(this, 1, i, PendingIntent.FLAG_NO_CREATE);

        if(pi.equals(pisecond))
            Log.v("check","they are equal");
        else
            Log.v("check", "they are not equal");

        notif.contentIntent=pisecond;

        nm.notify(11, notif);

As per docs , PendingIntent.FLAG_NO_CREATE does not create any new object if there is an existign object. i am printing value of number in the NotifResult activity wherein number value is coming to be 80 rather than 50 expected as it should use existing intent with old value ( as per my understanding).kindly update why output is coming 80. the log is showing objects to be equal as expected.

thanks

user2779311
  • 1,688
  • 4
  • 23
  • 31

1 Answers1

10

When you call:

PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);

passing 0 as the flags parameter means that you are setting no flags.


If you call:

PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);

again, and the Intent you pass matches the Intent from the first call, then you will get back the same PendingIntent as from the first call. "matches" means that the ACTION, DATA, CATEGORY and COMPONENT are all the same. Extras are not considered when matching.

If you provide different extras in the Intent for the second call, those extras will NOT be present in the PendingIntent when it is sent. The extras in the Intent from the first call will be used.


I cannot explain the behaviour you are seeing regarding "50" and "80". As per the docs and as per my understanding and as per my own observations, you should see "50" and not "80". There must be something else strange going on. Are you testing on a real device? an Emulator? What version of Android?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Thanks @Wasser .It was indeed helpful I dont remember now whether i was able to resolve this as it has been some time since i poted that. Anyhow your explanation still cleared a lot of things about this.Really helpful. Thanks – user2779311 May 08 '15 at 03:59