在开始时,Activity通过Intent启动并对该Intent执行某些操作。
当我更改Activity的方向时,它会重新加载,并将Intent传递给Activity。
我该如何清除该Intent以防止Activity再次使用它?
我尝试过setIntent(null)
,但没有效果。
在开始时,Activity通过Intent启动并对该Intent执行某些操作。
当我更改Activity的方向时,它会重新加载,并将Intent传递给Activity。
我该如何清除该Intent以防止Activity再次使用它?
我尝试过setIntent(null)
,但没有效果。
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. */
}
不要使用setIntent(null)
。尽管有时可能有效,但在某些情况下,原始intent可能会返回。
相反,调用setIntent()
使用一个简单的intent,它没有动作、数据或额外信息,例如new Intent(Context, Class)
。
实际上,一开始使用setIntent()
并不是一个好的API设计决策。正如dcg所指出的那样,在你的savedInstanceState
仍为空时,请确保只检查你的Intent第一次。
savedInstanceState == null
是太棒了!!!这帮助我区分我是从启动器还是从打开电子邮件附件接收到的意图开始我的MainActivity。 +1 - Nathan VanceKotlin 中:
如果你想清除 intent,那么这是最好的选择。
intent.replaceExtras(Bundle())
intent.action = ""
intent.data = null
intent.flags = 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
}
}
...
Intent i = getIntent();
i.setAction(null);
setIntent(i);
虽然这篇文章有些旧,但某些人可能仍会用得上。
不要浪费时间,如果您的应用被恢复,那么意图将再次出现。
在“Activity Resume”中使用起始意图,并添加一个额外的值即可。
putExtra("DONE", 0)
hasExtra("DONE")
易懂
getIntent().setData(null);
intent.putExtra(YourKey,""); //将值重置为未知
setIntent(intent);
我的建议是使用一个布尔变量来切换,以检查您的活动是否已被创建。
否则,您可以查看您的onCreate方法,据我所知,这仅在首次创建活动时执行。
onRetainNonConfigurationInstance()
将旧活动的状态传递到新活动中。 - CommonsWarepublic 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);
}
这样,就有了一种机制可以在保留原始Intent的能力的同时,将原始Intent的某些部分明确地保留下来。
请注意,我没有测试所有的Activity启动模式。