Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?
If only one of your PendingIntents
for this contact will be outstanding at any point in time, or if you always want to use the latest set of extras, use FLAG_UPDATE_CURRENT
when you create the PendingIntent
.
If more than one contact-specific PendingIntent
will be outstanding at once, and they need to have separate extras, you will need to add a count or timestamp or something to distinguish them.
intent.setAction("actionstring" + System.currentTimeMillis());
UPDATE
Also, the lightly-documented second parameter to getActivity()
and kin on PendingIntent
apparently can be used to create distinct PendingIntent
objects for the same underlying Intent
, though I have never tried this.
How can I correctly pass unique extras to a pending intent?
Possibly two different issues here:
1) If you've already created your PendingIntent before and it "matches" an existing PendingIntent, then you must specify the PendingIntent.FLAG_UPDATE_CURRENT flag or it will not pass the extras. A "match" is based on the criteria that Intent.filterEquals() uses, so definitely read the docs there and make sure you understand the data, action, type, etc.
2) I've read that if you do NOT set an action on your intent, then it will not propagate the extras, so perhaps try intent.setAction("com.blah.Action").
AppWidgets - Updating Intent extras contents dynamically
How this effect can be achieved?
Use an appropriate flag on your getActivity()
call, such as FLAG_UPDATE_CURRENT
, if you are creating a PendingIntent
on the same core Intent
(e.g., for the same activity) and are changing extras.
That being said, since TransparentPromptActivity
and MainActivity
are not the same activity, FLAG_UPDATE_CURRENT
will not help. Perhaps your issue is that incomingMessage
is null
in your second case.
Notification passes old Intent Extras when the Activity that read the extras values is on top
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();
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()
- 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);
}
Notification PendingIntent Intent extras are overridden by another notification
There are two ways to solve this:
One is to set a different action on the Intent. So in your example, you could set Intent1.setAction("Intent1")
and Intent2.setAction("Intent2")
. Since the action is different, Android will not override the extras on the intent.
However, there may be a case where you actually need to set a specific action on this intent (i.e. your action corresponds to a specific broadcast receiver). In this case, the best thing to do is set the request code to something different in each PendingIntent:
PendingIntent pendingIntent1 = PendingIntent.getActivity(context,
(int) System.currentTimeMillis() /* requestCode */, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context,
(int) System.currentTimeMillis() /* requestCode */, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
By setting a new request code on the PendingIntent, Android will not override the extras on each of their corresponding intents.
android: getting same value from Intent extras
This should answer you query for sure...cheers :)
Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?
Where are stored PendingIntents? Big trap of using
PendingIntent
s are stored in non-persistent storage. When the device restarts, they are all gone.
Also, if you create a PendingIntent
and put it in a Notification
, once the Notification
is gone (dismissed, opened, etc.) the PendingIntent
is no longer in use and will be deleted.
Generally you should not use random numbers for the requestCode
as this is no guarantee of uniqueness. You need to find a way to make your PendingIntent
unique if you want to have many of them existing in parallel.
Android Status Bar Notifications - Intent getting the old extras on the second time
You need to set up a clean flag to PendingIntent
:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Have a look at this post that gives a longer explanation.
Related Topics
How to Add Buttons at Top of Map Fragment API V2 Layout
Failed to Resolve Com.Android.Support:Appcompat-V7:22 and Com.Android.Support:Recyclerview-V7:21.1.2
Set the Layout Weight of a Textview Programmatically
How to Run Code on a Background Thread on Android
Sending a Notification from a Service in Android
Actionbar - Custom View with Centered Imageview, Action Items on Sides
Android Fade in and Fade Out with Imageview
How to Add Footer to Navigationview - Android Support Design Library
Android - Volume Buttons Used in My Application
Detecting When User Has Dismissed the Soft Keyboard
Download the Android Sdk Components for Offline Install
Redraw a Single Row in a Listview