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:57

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. */
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-11 23:59

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-03-11 23:59

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);
查看更多
登录 后发表回答