在 Android 中启动新活动并完成当前活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11308198/
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:51:33  来源:igfitidea点击:

Start new Activity and finish current one in Android?

androidandroid-activityflagsstart-activity

提问by pixel

Currently I'm starting a new Activityand calling finishon a current one.

目前我正在开始一个新的Activity并呼吁finish当前的。

Is there any flag that can be passed to Intentthat enables finishing current Activitywithout a need to call finishmanually from code?

是否有任何标志可以传递到无需从代码手动调用Intent即可完成当前操作?Activityfinish

回答by Simon Dorociak

You can use finish()method or you can use:

您可以使用finish()方法,也可以使用:

android:noHistory="true"

And then there is no need to call finish()anymore.

然后就没有必要再打电话finish()了。

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

回答by Avi Kumar Manku

Use finishlike this:

finish像这样使用:

Intent i = new Intent(Main_Menu.this, NextActivity.class);
finish();  //Kill the activity from which you will go to next activity 
startActivity(i);

FLAG_ACTIVITY_NO_HISTORYdoes the opposite. New activity is not saved in history, whereas OP wants previous Activity being finished.

FLAG_ACTIVITY_NO_HISTORY相反。新活动不会保存在历史记录中,而 OP 想要完成之前的活动。

To learn more on using Intent.FLAG_ACTIVITY_NO_HISTORYread: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY

要了解有关使用Intent.FLAG_ACTIVITY_NO_HISTORY阅读的更多信息:http: //developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY

回答by 10s

FLAG_ACTIVITY_NO_HISTORY when starting the activity you wish to finish after the user goes to another one.

FLAG_ACTIVITY_NO_HISTORY 在用户转到另一个活动后开始您希望完成的活动时。

http://developer.android.com/reference/android/content/Intent.html#FLAG%5FACTIVITY%5FNO%5FHISTORY

http://developer.android.com/reference/android/content/Intent.html#FLAG%5FACTIVITY%5FNO%5FHISTORY