[英]Destroy activity when creating the same activity
I have 2 activities A and B, A is defined as "singletask" on the manifest launch mode, as it should be and i cannot change this. 我有2个活动A和B,A被定义为清单启动模式中的“单任务”,这应该是正确的,我无法更改。 A launches activity B, then when B clicks the back button i want to launch activity A again, but i want to destroy the previous activity, and create a new instance of A. right now the following code is not working.
A启动活动B,然后当B单击后退按钮时,我想再次启动活动A,但是我想销毁上一个活动,并创建A的新实例。现在,以下代码不起作用。 It just takes me back to the old A activity.
它只是带我回到了旧的A活动。
<activity
android:name="A"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" />
public void onBackPressed() {
Intent intent = new Intent(this,A.class);
intent.putExtra("newextra1","newextra1");
intent.putExtra("newextra2","newextra2");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
Just call finish()
in Activity A after you call the intent for Activity B. It will be executed even if another Intent is started before calling finish(). 只需调用
finish()
在活动A你叫即使另一个目的是呼吁结束之前开始将执行的活动B.意图后 ()。
You are calling finish()
in Activity B but not Activity A. 您正在活动B中调用
finish()
,而不是活动A。
if you want the old activity in some cases and new in some, then just pass a flag(boolean) in intent that tells the activity to restart or not. 如果您想在某些情况下使用旧的活动,而在某些情况下使用新的活动,则只需传递一个意图的标志(布尔值)即可告诉活动是否重新启动。 Then if the flag is yes you do
然后,如果标志为是,则执行
finish();
startActivity(getIntent());
in Activity A. 在活动A中。
尝试:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.