How to clear Intent that started Activity?

后端 未结 9 1161
攒了一身酷
攒了一身酷 2021-02-03 21:07

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

相关标签:
9条回答
  • 2021-02-03 21:41

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

    getIntent().setData(null);
    
    0 讨论(0)
  • 2021-02-03 21:42

    I had similar problem. This helped me. Maybe you have to also use onSaveInstanceState(Bundle outState) and add extra data to the bundle so inState is not null, not sure.

    Intent activityIntent = null; // Subsequent times it's null
    
    @Override 
    protected void onCreate(Bundle inState) {
        super.onCreate(inState);
        .
        .
        if (inState!=null) {
            /*Recreating activity with a saved state*/
        } else {
            /*Creating activity*/
            activityIntent = getIntent(); // First time through intent is used
            /*Get your Extra Intent data here. You will be capturing data on 1st creation. */
    }
    
    0 讨论(0)
  • 2021-02-03 21:45

    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.

    0 讨论(0)
  • 2021-02-03 21:46

    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.

    0 讨论(0)
  • 2021-02-03 21:48

    Do not use setIntent(null). It seems that while it may work sometimes, under some conditions the original intent may come back.

    Instead, call setIntent() with a simple intent that doesn't have an action, data or extras, such as new Intent(Context, Class).

    Indeed, the use of setIntent() was actually not a good API design decision in the first place. Instead, as dcg noted, be sure you only check your intent the first time, when your savedInstanceState is still null.

    0 讨论(0)
  • 2021-02-03 21:48

    If your intent is sent to your activity with an action (with setAction), just do the following when you receive the intent, to avoid multiple handling of this intent when screen rotates :

    Intent i = getIntent();
    i.setAction(null);
    setIntent(i);
    
    0 讨论(0)
提交回复
热议问题