简体   繁体   English

启动另一个呼叫者时完成呼叫者活动

[英]finish caller activity when starting another one

在此处输入图片说明 like in the diagram I want to close all previous activities when starting another one. 就像在图中一样,我想在启动另一个活动时关闭所有先前的活动。 I tried a lot with flags, but it doesn't seems to work. 我用标志尝试了很多,但是似乎不起作用。 eg. 例如。 when starting Activity B on Activity AI called 在活动AI上启动活动B时

Intent intent = new Intent(this, ActivityB.class);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

or 要么

Intent intent = new Intent(this, ActivityB.class);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

But the finish() Method isn't called in Activity A. I've got a global class, called Navigation which extends Activity to handle things like this in one class. 但是在Activity A中没有调用finish()方法。我有一个名为Navigation的全局类,该类将Activity扩展为在一个类中处理类似的事情。 All orange Activities extends my Navigation, the blue ones don't. 所有橙色的活动都会扩展我的导航,蓝色的活动则不会。 BackButton behaviour seems to work fine, but the finish() method isn't called when starting another activity. BackButton的行为似乎可以正常工作,但是在启动另一个活动时未调用finish()方法。 Do I have to explicitly call finish() ? 我必须显式调用finish()吗? Where? 哪里? If I do it in onStop I can't go back to Activity X when restarting the app. 如果我在onStop执行此操作,则在重新启动应用程序时无法返回到活动X。

After

startActivity(intent);

simply put 简单的说

finish();

in activity that you want to finish. 在您想要完成的活动中。 Flags are needed only when ie. 仅当ie时才需要标记。 you want to clear activity stack 您想清除活动堆栈

You are doing right just add finish() in your code like that: 您做得对,只需在代码中添加finish()即可:

Intent intent = new Intent(this, ActivityB.class);
intent = intent = intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM