Gustavomcls
Gustavomcls

Reputation: 1655

Notification passes old Intent Extras when the Activity that read the extras values is on top

Notification is giving old values. I read the stackoverflow link but still not working for me: Notification passes old Intent Extras

I have a Activity A. When I am on the activity B and touch the Notification, the Extra parameter is given correctly and shows the Activity A with the correctc values read with getExtras(..);

Then the Activity A is still on the top - showing on the screen: I click on the second notification with new values of putExtras(newValue) to create a new activity A but with new values.

The problem: The intent.getExtras()` is reading old values of the first notification clicked instead new values given by the second notification.

I made a lot of combinations of Flags of Pending Intent and the combinations of the link on top, but the aplication is still taking the old values(the values of the first touched notification) for the second Notification. I tried the flag: PendingIntent.FLAG_UPDATE_CURRENT to update the values instead create a new one Activity and some others Flags.

How can I make the second notification give the correct values for the activity A when the Activity A is still shown on the screen?

The snippet of the code creating the notification.

   public void notificationCreateGu(String newMessageUserUidOfSender) {


        Intent it = new Intent(this,ActivityA.class);
        it.putExtra(USER_UID_READER,newMessageUserUidOfSender);
        StoreValuesClass.count=StoreValuesClass.count+2;
        PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setTicker(newMessageUserUidOfSender )
                .setSmallIcon(android.R.mipmap.sym_def_app_icon)
                .setContentTitle("Title Message ")
                .setContentText(String.valueOf(newMessageUserUidOfSender))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();



        int m;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         m= StoreValuesClass.count=StoreValuesClass.count+2;
        notificationManager.notify((m), notification);
}

//StoreValueClass.count is a static values that can be read by the activity to give an unique id for the notification.

the snippet of code reading the values.

userUidReader = getIntent().getExtras().getString(USER_UID_READER)

I tried to reload the values into onResume() but into onResume() the still taking the old values of the first time the getExtras() is read. i understood that the operational system Android are not creating a new Activity but only giving it to the top.

Upvotes: 1

Views: 1646

Answers (2)

Gustavomcls
Gustavomcls

Reputation: 1655

using the answser of CommonsWare that helped with the override onNewIntetn and the link: http://www.helloandroid.com/tutorials/communicating-between-running-activities

1 into xmlFile: put th android:launchMode="singleTask" for the activity will receive the extra parameters with getExtras.

 <activity android:name=".ActivityWillReceiveWithGetExtras"
   android:launchMode="singleTask"
   android:taskAffinity=""
   android:excludeFromRecents="true">
</activity>

2.into the activity you that will receive the values with get_extras(...) override a method called onNewIntent:

2.1 Observation: put the line:

setIntent(intent);

to set the identifier of the intent.

   @Override
   protected void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
              setIntent(intent);//must store the new intent unless getIntent() will return the old one
        getExtraParameterActual();

}

2.2 get the Extra parameters into a function that will have inside the command getExtras(...

 getExtraParameterActual();
  1. Write the function of the top getExtraParameterActual();

    private void getExtraParameterActual() { Intent intent = getIntent();//take back the value set with //setintenT of pass 2.1 user = getIntent().getExtras().getString(USER);// }

5. into OnCreate() call the e getExtraParameterActual(); and if necessary reload your views with a method for example reloadMyViews()

  1. into onResume() reload your views again with the same function of the pass 5 reloadMyViews()

7 the notificatio code I used take care with the FLAGS

 public void notificationCreateGu(String User) {

        Log.d(TAG,nameOfTheService+"BUG createnotification for received CHAT messages useruidOfTheFriendNear="+newMessageUserUidOfSender);
        Intent it = new Intent(this,ActivityWillReceiveWithGetExtras.class);
        it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        it.putExtra(USER,user);
        StoreValuesClass.count=StoreValuesClass.count+2;
        PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationCompat.Builder(this)
                .setTicker(newMessageUserUidOfSender )
                .setSmallIcon(android.R.mipmap.sym_def_app_icon)
                .setContentTitle("Title Message ")
                .setContentText(String.valueOf(newMessageUserUidOfSender))
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();



        int m;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         m= StoreValuesClass.count=StoreValuesClass.count+2;
        notificationManager.notify((m), notification);

    }

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1007379

Override onNewIntent() in your activity.

getIntent() returns the Intent that was used to initially create the activity. If an existing activity instance is brought back to the foreground via a startActivity() call, onNewIntent() is called to deliver to you the Intent used for that most recent startActivity() call.

Upvotes: 1

Related Questions