My Android app is getting called by an intent that is passing information (pendingintent in statusbar).
When I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
this is the code that doesn't run like its supposed to
String imgUrl;
Bundle extras = this.getIntent().getExtras();
if(extras != null){
imgUrl = extras.getString("imgUrl");
if( !imgUrl.equals(textView01.getText().toString()) ){
imageView.setImageDrawable( getImageFromUrl( imgUrl ) );
layout1.setVisibility(0);
textView01.setText(imgUrl);//textview to hold the url
}
}
And my intent:
public void showNotification(String ticker, String title, String message,
String imgUrl){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(ns);
int icon = R.drawable.icon; // icon from resources
long when = System.currentTimeMillis(); // notification time
CharSequence tickerText = ticker; // ticker-text
//make intent
Intent notificationIntent = new Intent(this, activity.class);
notificationIntent.putExtra("imgUrl", imgUrl);
notificationIntent.setFlags(
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT);
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT);
//make notification
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, message, contentIntent);
//flags
notification.flags = Notification.FLAG_SHOW_LIGHTS |
Notification.FLAG_ONGOING_EVENT |
Notification.FLAG_ONLY_ALERT_ONCE |
Notification.FLAG_AUTO_CANCEL;
//sounds
notification.defaults |= Notification.DEFAULT_SOUND;
//notify
mNotificationManager.notify(1, notification);
}
Is there any way to clear the intent or check whether it has been used before?
This will hopefully help everyone. So first we get the intent
Place this somewhere onCreate
Now lets set this so everytime our app gets destroyed or exited we will remove the data
You get the idea, if this is not enough just find more 'on' callbacks
While the
Intent.removeExtra("key")
will remove one specific key from the extras, there is also the method Intent.replaceExtras(Bundle), which can be used to delete the whole extras from the Intent, ifnull
is passed as a parameter.From the docs:
Because the putXXX() methods initialize the extras with a fresh Bundle if its null, this is no problem.
Make sure that you are using PendingIntent.FLAG_UPDATE_CURRENT flag for PendingIntent.
Where
mPutIntent
is yourIntent
.Hope this will help you.
I couldn't find a way to remove Intent Extra. None of the answers about removing extra from intent works if you enable "Do Not Keep Activities" from Developer Options (That way you can destroy Activity and come back to test if Extras are still there).
As a solution to the problem, i stored boolean value in SharedPreferences after Intent Extras are processed. When same Intent is redelivered to the Activity, i check the SharedPreference value and decide to process the Intent Extra. In case you send another new Intent Extra to same Activity, you make SharedPreference value false and Activity will process it. Example:
Clearing an intent object:
The straight forward way is to avoid calling getIntent() from methods other than onCreate(). But this will cause problem during next launch if the user left our Activity by tapping the Home button. I think this problem don't have a fully functional solution.