离开时如何杀死Android活动,以便无法从后退按钮访问它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2550099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:17:14  来源:igfitidea点击:

How to kill an Android activity when leaving it so that it cannot be accessed from the back button?

android

提问by JohnRock

In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is stale and I want to remove it completely so it can not be accessed again from the back button.

在给定的 Android 活动中,我想在某个时候为用户启动一个新活动。一旦他们离开第一个活动并到达第二个活动,第一个活动就过时了,我想将其完全删除,以便无法通过后退按钮再次访问它。

How is the best way to accomplish this? How do I kill or destroy this activity immediately after the user has launched the new activity?

实现这一目标的最佳方法是什么?如何在用户启动新活动后立即终止或销毁此活动?

回答by CaseyB

You just need to call finish()

你只需要打电话 finish()

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();

回答by jqpubliq

Setting android:noHistory="true"on the activity in your manifest will remove an activity from the stack whenever it is navigated away from. see here

设置android:noHistory="true"清单中的 Activity 将在它被导航离开时从堆栈中删除它。看这里

回答by Jmorvan

you can use:

您可以使用:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

回答by Danke Xie

You can also add android:noHistory="true"to your Activitytag in AndroidManifest.xml.

您还可以添加android:noHistory="true"到您Activity的标签AndroidManifest.xml

<activity
            ...
            android:noHistory="true">
</activity>

回答by RobGThai

Yes, all you need to do is call finish() in any Activity you would like to close.

是的,您需要做的就是在要关闭的任何活动中调用 finish() 。

回答by JBL AK-47

Write this in each "new activity" after you initialized your new intent->

初始化新意图后,将其写入每个“新活动”中->

Intent i = new Intent(this, yourClass.class);
startActivity(i);
finish();

回答by kdblue

Finally, I got a solution!

终于,我得到了一个解决方案!

My Context is:- I want disconnect socket connection when activity destroyed, I tried to finish() activity but it didn't work me, its keep connection live somewhere.

我的上下文是:- 当活动被破坏时,我想断开套接字连接,我试图完成()活动,但它对我不起作用,它使连接保持在某处。

so I use android.os.Process.killProcess(android.os.Process.myPid());its kill my activity and i used android:excludeFromRecents="true"for remove from recent activity .

所以我用 android.os.Process.killProcess(android.os.Process.myPid());它杀死我的活动,我用来android:excludeFromRecents="true"从最近的活动中删除。

回答by Mayuran Balasubramaniam

Add this attribute to you activity in manifest file. android:noHistory="true"

将此属性添加到清单文件中的活动。机器人:noHistory =“真”

回答by Soni Kumar

You just need to use below code when launching the new activity.

您只需要在启动新活动时使用以下代码。

startActivity(new Intent(this, newactivity.class));
finish();