[英]How to remove activity from activity backstack?
I have two activities Activity A and Activity B. When I click button in Activity A, The Activity B starts. 我有两个活动活动A和活动B.当我单击活动A中的按钮时,活动B开始。 Now When I press back button from Activity B the Activity A get restarted.
现在,当我按下活动B的后退按钮时,活动A重新启动。 But I want to come out of the app when the back button in Activity B is pressed.
但是,当按下活动B中的后退按钮时,我想退出应用程序。 I tried using this but not getting success
我试过用这个但没有成功
Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Thanks in advance 提前致谢
You need to finish ActivityA
once you're starting ActivityB
: 你需要完成
ActivityA
一旦你开始ActivityB
:
Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Call finish() after starting new Activity - this will close the calling Activity: 启动新活动后调用finish() - 这将关闭调用Activity:
startActivity(new Intent(ActivityA.this, ActivityB.class));
//calling finish() closes current Activity
finish();
Read more about Activity life cycle here and here . 了解更多关于活动的生命周期在这里和这里 。
Just call finish() after startActivity(intent). 只需在startActivity(intent)之后调用finish()。
Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Intent.FLAG_ACTIVITY_CLEAR_TOP
is not suitable for your situation.In your code,ActivityA will re-builded again.There has two instances of ActivityA in the TaskStack.Delete the addFlag. Intent.FLAG_ACTIVITY_CLEAR_TOP
不适合您的情况。在您的代码中,ActivityA将再次重新构建.ActiveStack中有两个ActivityA实例。删除addFlag。
Intent intent=new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
finish();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.