How to clear Intent that started Activity?

2019-03-11 23:00发布

At the beginning Activity is launched by an Intent and something is done with this Intent.

When I change orientation of my Activity, it's reloaded again and Intent is passed to the Activity.

How can I clear that Intent to prevent Activity from using it again?

I've tried setIntent(null), but with no result.

9条回答
狗以群分
2楼-- · 2019-03-11 23:38

My suggestion would be to toggle this with a boolean variable to check wheather your activity is first created or not.

Otherwise you can look at your onCreate method, afaik this is only executed, when the activity is first created.

查看更多
beautiful°
3楼-- · 2019-03-11 23:45

intent.putExtra(YourKey,""); //reset the value to knknown setIntent(intent);

查看更多
一纸荒年 Trace。
4楼-- · 2019-03-11 23:46

In my case I needed to set the data to null:

getIntent().setData(null);
查看更多
We Are One
5楼-- · 2019-03-11 23:51

Old post, but some one could use this.

Dont waste time, if your app is resumed, the intent will be there again.

Use the starting intent on the "Activity Resume", and just add an extra value

putExtra("DONE", 0)

And each time your app resumes, check if already has this value:

hasExtra("DONE")

easy

查看更多
啃猪蹄的小仙女
6楼-- · 2019-03-11 23:51

If you set the intent action, you can clear it with getIntent().setAction("");

For example in onCreate(...):

...
String action = getIntent().getAction();
if (action != null) {
  if (action.equals(YOUR_ACTION_WHATEVER)) {
    doSomethingHere(); // do something here
    getIntent().setAction(""); // clear the action
  }
}
...

This will clear the action, otherwise it will be called every time you rotate the device.

查看更多
甜甜的少女心
7楼-- · 2019-03-11 23:52

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) {
    super.onCreate(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));
    }

    processIntent(getIntent())
}

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

    doSomething(intent.getExtras.getString("SOMETHING_I_REALLY_NEED_TO_PERSIST"));

    final String somethingIDontWantToPersist = 
        intent.getExtras.getString("SOMETHING_I_DONT_WANT_TO_PERSIST");

    if(somethingIDontWantToPersist != null) {
        doSomething(somethingIDontWantToPersist);
    }
}

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

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.

查看更多
登录 后发表回答