2010-06-30 102 views
23

开始时活动由Intent启动,并且使用此Intent完成某些操作。如何清除启动Activity的意图?

当我更改我的Activity的方向时,它会再次重新加载,并将Intent传递给Activity。

如何清除该意图以防止活动再次使用它?

我试过setIntent(null),但没有结果。

+0

像素应该不接受明显错误的答案并接受@ dcg的代替。那会为我节省一个小时的工作路线。请参阅Dianne Hackborn关于setIntent是如何的帖子:https://groups.google.com/forum/#!msg/android-developers/vrLdM5mKeoY/ThkOonAbtloJ – JohnnyLambada 2015-02-03 21:26:36

+0

看看这是否有用 - stackoverflow.com/a/19820057/313042 – rajath 2017-09-06 07:12:42

回答

17

我有类似的问题。这帮助了我。 也许你还必须使用onSaveInstanceState(Bundle outState)并向bundle添加额外的数据,所以inState不为空,不确定。

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

我在下面有一个类似的方法,也可以让你从最初的意图中保留选择性参数。 – dell116 2017-02-26 03:56:02

+1

从退出应用程序退出后从最近的应用程序列表中打开时,这对我无效。该应用程序将打开一个空包,但前一个意图。 – CACuzcatlan 2017-07-14 20:24:37

0

我的建议是用布尔变量切换来检查你的活动是否是第一次创建的。

否则,您可以查看您的onCreate方法,afaik仅在第一次创建活动时执行。

+1

同意。使用'onRetainNonConfigurationInstance()'将状态从旧活动传递给新活动。 – CommonsWare 2010-06-30 11:45:17

+3

实际上,当屏幕方向改变时,onCreate()也会被调用(应用程序基本上被销毁并在此期间重新创建)。 – 2010-07-01 08:09:14

1

如果你的意图被发送到你的活动与动作(带的setAction),只是下面当您收到的意图,以避免这种意图的多重处理时,屏幕旋转:

Intent i = getIntent(); 
i.setAction(null); 
setIntent(i); 
+0

不起作用 - 用api 25检查。 – 2017-10-06 19:38:24

8

不要使用setIntent(null)。看起来虽然它有时可能有效,但在某些情况下,最初的意图可能会回来。

取而代之的是,调用 setIntent()的简单目的是没有动作,数据或额外信息,例如 new Intent(Context, Class)

事实上,使用setIntent()其实并不是一个好的API设计决定。相反,正如dcg指出的那样,确保您只在第一次检查您的意图时,savedInstanceState仍为空。

+2

setIntent就在那里让你感到困惑 - 不要使用它。请参阅@ dcg的答案。当一个Activity被销毁时,无论你使用setIntent做什么,都会重新使用启动该Activity的原始意图。原始的Intent完全保留在应用程序之外,在ActivityManager中,setIntent对它没有影响。请参阅Dianne Hackborn的帖子:https://groups.google.com/forum/#!msg/android-developers/vrLdM5mKeoY/ThkOonAbtloJ – JohnnyLambada 2015-02-03 21:23:58

+0

感谢您对该帖子的引用@JohnnyLambada!更新答案以避免误导人们。 – 2015-02-03 21:50:05

+0

检查'savedInstanceState == null'是否是BRILLIANT !!!这有助于区分我是从启动器启动MainActivity,还是通过打开电子邮件附件获得收到的意图。 +1 – 2015-07-15 21:10:20

0

在我来说,我需要将数据设置为null:

getIntent().setData(null); 
2

老帖子,但有些人可以使用它。

不要浪费时间,如果你的应用程序恢复,意图将再次出现。

使用开始一心想“活动继续”,只是增加一个额外的价值

putExtra("DONE", 0) 

和各个应用程序恢复时间,检查是否已经有这个值:

hasExtra("DONE") 

容易

+0

这不起作用。我认为Android以不同的方式保存'Intent',而不是内存。你只是在运行时添加这个值。当活动被杀死后,系统再次检索原始的“意图”。 – 2018-01-22 02:25:30

0

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

0

即使在手动清除Int ent和Intent extras在它们被解析后,似乎Activity.getIntent()将始终返回启动Activity的原始Intent。

要解决这个问题,我建议是这样的:

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); 
} 

这种方式,有倾倒的初衷,同时仍可以明确地保留原来的意图/意向的某些部分的能力的机制临时演员。

请注意,我没有测试过所有的Activity启动模式。

0

如果你设置的意图动作,你可以用getIntent().setAction("");

例如在onCreate(...)清除:

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

这将清除行动,否则它会被称为每次旋转设备时。