Home » Android » android – Clearing intent

android – Clearing intent

Posted by: admin March 11, 2020 Leave a comment


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.

    public void onSaveInstanceState(Bundle savedInstanceState) {
    public void onRestoreInstanceState(Bundle 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 ) );
            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);
        PendingIntent.FLAG_UPDATE_CURRENT | 
    PendingIntent contentIntent = 
        PendingIntent.getActivity(this, 0, 
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | 

    //make notification
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(this, title, message, contentIntent);
    notification.flags = Notification.FLAG_SHOW_LIGHTS | 
        Notification.FLAG_ONGOING_EVENT | 
        Notification.FLAG_ONLY_ALERT_ONCE | 
    notification.defaults |= Notification.DEFAULT_SOUND;
    mNotificationManager.notify(1, notification);

Is there any way to clear the intent or check whether it has been used before?

How to&Answers:


I didn’t realise this answer would be referred to so much when I first wrote it more then 5 years ago!

I’ll clarify to point out that as per @tato-rodrigo answer this won’t help you detect an already handled intent in some situations.

Also I should point out I put “clear” in quotes for a reason – you are not really clearing the intent by doing this, you’re just using the removal of the extra as a flag that this intent has been seen by the activity already.

I had exactly the same issue.

Above answer put me on the right track and I found even simpler solution, use the:


method call to “clear” the Intent.

Its a bit late answering since this was asked a year ago, but hopefully this helps others in the future.


EDIT: I’m editing to post the complete solution I’m using.

This solution will work if the problem is “Not execute some code when the activity starts from History (Recent Apps)”.

First of all, declare a boolean in your Activity to indicate if the Intent was already consumed:

    private boolean consumedIntent;

Then, safely store and restore this value using the onSaveInstanceState and onCreate methods to handle configuration changes and cases that the system may kill your Activity when it goes to background.


    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(SAVED_INSTANCE_STATE_CONSUMED_INTENT, consumedIntent);

    protected void onCreate(Bundle savedInstanceState) {
        //set content view ...

        if( savedInstanceState != null ) {
            consumedIntent = savedInstanceState.getBoolean(SAVED_INSTANCE_STATE_CONSUMED_INTENT);

        //other initializations

Now, check if you can run your code under onResume method.

    protected void onResume() {

        //check if this intent should run your code
        //for example, check the Intent action
        boolean shouldThisIntentTriggerMyCode = [...];
        Intent intent = getIntent();
        boolean launchedFromHistory = intent != null ? (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0 : false;
        if( !launchedFromHistory && shouldThisIntentTriggerMyCode && !consumedIntent ) {
            consumedIntent = true;
            //execute the code that should be executed if the activity was not launched from history

Additionally, if your Activity is configured to singleTop, you should reset your flag when a new Intent is delivered.

    protected void onNewIntent(Intent intent) {
        consumedIntent = false;


Maks answer works for clearing an extra:


Another useful command is:


You could also tag an intent by calling:

    getIntent().putExtra("used", true);

and then just check for the value.


When we launch Android apps from History (Recent Apps), the app could be launched with primarily three different Intent flags.

    This is when activity is being launched from history of an app that was minimised (long press home key).
    Constant Value: 1048576 (0x00100000)
    This is when activity is launched via “clicking application icon” or via “Intent filters“. Here the activity will become the start of a new task on this history stack.
    Constant Value: 268435456 (0x10000000)
    This is when app was quit by pressing back button and then resumed from History (recent apps).
    Constant Value: 269484032 (0x10100000)

Constant value can be retrieved via getIntent().getFlags()

In the third case, Android reloads the last Intent values from its memory. So your app’s intent (getIntent) will have values from the last intent that launched the app.

Actually, the app should behave as if it is a fresh launch, with intents values for a fresh launch rather than the intent values of the previous launch. This behaviour can be seen if you launch the app by clicking the app icon, it will never have old intent values. This is because, Android uses the following intent filter for this scenario

        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER"/>

But in the third case (App that was quit, is launched from History of Recent apps), Android OS uses that last intent that launched the app before it was quit (via pressing back button). So you end up having old intent values and the app flow is not proper.

Removing intent is one way of solving it, but it would not solve the issue completely! As Android OS reloads the Intent from the apps last launch, and not last instance of the launch intent.

A clean way to avoid this from happening, is to handle it via getting the type of Intent to determine the launch type.

So in your LaunchActivity (The one which has the intent filter defined in the manifest), you could use the following code in the onCreate(), onStart(), or onResume() methods.

if(getIntent().getFlags() == (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)) {
    //app is launched from recent apps after it was closed
    } else {
        String intentAction = getIntent().getAction();
        String scheme = getIntent().getScheme();
        //app is launched via other means
        // URL intent scheme, Intent action etc
        if("https".equalsIgnoreCase(scheme)) {
            // URL intent for browser
        } else if("com.example.bb".equalsIgnoreCase(intentAction)) {
            // App launched via package name
        } else {
            // App was launched via Click on App Icon, or other means

I presume normalLaunch(), should not use parameters from the Intent; otherwise you would need to segregate and optimise your default launch method not to use Intent parameters.


Clearing an intent object:

intent.replaceExtras(new Bundle());


Short answer is No way

Long answer. There is no such thing as “one-shot” intent. From experiment it is observed that recent activity history in modern Androids is nothing more than “Intent history”. The latest intent passed to activity is simply logged into system and that’s the deal. Folks above suggest to use


But it does not work because the intent is already logged till the moment you get it inside onNewIntent() or onStart() method.

I solved the problem by avoiding use of intents. My problem was simmiliar to the one posted by the author. I tried to implement Global Exit from application via control in notification area. It should stop the underlying service and close all activities of the app. You can find same behavior in Waze application.

The algorithm:

  1. Create PendingIntent for notification control which passes “Exit” action to activity. But to the special activity which is a simple proxy.
  2. Proxy activity onStart() code parses the intent, checks action and sets the state of some model to “Exited”.
  3. Proxy activity onStart() code clears original intent using setIntent(“”) and then forwards it to destination “Root” activity by calling startActivity(intent).
  4. Proxy activity onStart() code invoke finish().
  5. Inside onStart() and onNewIntent() of destination activity check model state and call finish() if it is “Exited” (and also call stopService() in my case).

I hope this will help to someone because I did not find the answer in the internet.


Make sure that you are using PendingIntent.FLAG_UPDATE_CURRENT flag for PendingIntent.

PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, mPutIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Where mPutIntent is your Intent.

Hope this will help you.


I’ve got exactly the same problem. My solution was to add boolean variable that was set when Intent was ‘used’ and if statement based on this boolean to check if you should use Intent or not.


When you are done processing the Intent, do this:


You won’t see that processed Intent again, and you won’t be masking the problem by editing the contents of the processed Intent.


I recently had this problem and I solved it by adding a timestamp as a extra parameter to the intent:

private void launchActivity(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    intent.putExtra("KEY_EXTRA_TIMESTAMP", System.currentTimeMillis());

After that, save the timestamp to the shared preferences:

protected void onCreate(Bundle savedInstanceState) {

    long time = getIntent().getLongExtra("KEY_EXTRA_TIMESTAMP", -1);
    long previousTime = getPreferences(MODE_PRIVATE).getLong("timestamp", -1);

    //ignore if the timestamp is the same as the previous one  
    if (time != previousTime) {
        if (time != -1) {
            //save the timestamp
            getPreferences(MODE_PRIVATE).edit().putLong("timestamp", time).apply();


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:

// Start Activity with Intent Extras
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("someData", "my Data");
// Set data as not processed
context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).edit().putBoolean("myActivityExtraProccessed", false).commit();


public class MyActivity{

    public void someMethod(){
        boolean isExtrasProcessed = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).getBoolean("myActivityExtraProccessed", false);  
         if (!isExtrasProcessed) {
              // Use Extras

              //Set data as processed
              context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).edit().putBoolean("myActivityExtraProccessed", true).commit();



Even after manually clearing the Intent and Intent extras after they have been parsed, it seems as though Activity.getIntent() will always return the original Intent that started the Activity.

To get around this, I recommend something like this :

public void onCreate(Bundle savedInstanceState) {

    // The Intent provided by getIntent() (and its extras) will persist through a restore
    // via savedInstance.  Because of this, restoring this activity from a
    // an instance that was originally started with extras (deep-link or 
    // pre-defined destination) may cause un-desired behavior
    // (ie...infinite loop of sending the user directly to somewhere else because of a
    // pre-defined alternate destination in the Intent's extras).
    // To get around this, if restoring from savedInstanceState, we explicitly
    // set a new Intent *** to override the original Intent that started the activity.***
    // Note...it is still possible to re-use the original Intent values...simply
    // set them in the savedInstanceState Bundle in onSavedInstanceState.
    if (savedInstanceState != null) {
        // Place savedInstanceState Bundle as the Intent "extras"
        setIntent(new Intent().putExtras(savedInstanceState));


private void processIntent(Intent intent) {
    if (getIntent().getExtras() == null) {
        // Protection condition


    final String somethingIDontWantToPersist = 

    if(somethingIDontWantToPersist != null) {

public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save selective extras from original Intent...
    savedInstanceState.putString("SOMETHING_I_REALLY_NEED_TO_PERSIST", "persistedValued");

This way, there is a mechanism to dump the original Intent while still retaining the ability to explicitly retain certain parts of the original Intent / Intent extras.

Note that I haven’t tested all Activity launch modes.


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.


I confront the same problem and try to use above methods but it doesn’t work.

I think it may be cause of acitvity’s launch mode which I used singleTop mode.

When I background app and use RamEater to simulate issue that intent always has extra even I set it null or remove key.

The problem is gone by using preference storage on Android to check that had pass through.


How about this? Sets newIntent as the intent.

protected void onNewIntent(Intent intent) {


How about when you wish to clear the intent – replace it with an empty one?


protected void onNewIntent(Intent intent) {

public void onResume() {

    Intent theIntent = getIntent();
    if ("myaction".equals(theIntent.getAction()) {
         onNewIntent(new Intent());  // <--- "clear" the intent by setting empty one


This will hopefully help everyone.
So first we get the intent

Intent myIntent;

Place this somewhere onCreate

myIntent = getIntent();
String data = myIntent.getStringExtra("yourdata");
//Your process here

Now lets set this so everytime our app gets destroyed or exited we will remove the data

protected void onDestroy() {
    //TODO: Clear intents
protected void onBackPressed() {
    //TODO: Clear intents

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, if null is passed as a parameter.

From the docs:

Completely replace the extras in the Intent with the given Bundle of


extras The new set of extras in the Intent, or null to erase all extras.

Because the putXXX() methods initialize the extras with a fresh Bundle if its null, this is no problem.


Intent intent = new Intent(Intent.ACTION_MAIN);