问题描述
我有一个包含多个页面的应用程序,即多个活动,其中一些保持打开状态.
I have an application with multiple pages i.e., multiple activities and some of them remain open.
有没有办法一次关闭所有活动?
Is there a way to close all activities at once?
推荐答案
每当您希望退出所有打开的 Activity 时,您都应该按下一个按钮,该按钮加载应用程序启动时运行的 第一个 Activity 然后清除所有其他活动,然后完成最后一个剩余活动.为此,请在您的项目中应用以下代码
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish.to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
以上代码完成了除FirstActivity之外的所有Activity.然后我们需要完成FirstActivity的在 Firstactivity 的 oncreate 中输入以下代码
The above code finishes all the activities except for FirstActivity.Then we need to finish the FirstActivity'sEnter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
你就完成了......
and you are done....
这篇关于一次完成所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!