如何完成所有活动并关闭android中的应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20210691/
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
how to finish all activities and close the application in android?
提问by rahul
My application has the following flow:
我的应用程序有以下流程:
Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3
Home->screen 1->screen 2->screen 3->screen 4->screen 5>Home->screen 2->Home->Screen 3
My problem is that when I am trying to close the application then Home activity opens everytime when I am trying to close the application.
我的问题是,当我尝试关闭应用程序时,每次尝试关闭应用程序时都会打开 Home 活动。
I just want to close the application when user presses the back key of device on home screen.
当用户在主屏幕上按下设备的返回键时,我只想关闭应用程序。
回答by Ragaisis
There is finishAffinity()method that will finish the current activity and all parent activities, but it works only in Android 4.1 or higher.
有finishAffinity()方法可以完成当前活动和所有父活动,但它仅适用于Android 4.1 或更高版本。
回答by Loi Ho
This works well for me.
这对我很有效。
You should using
FLAG_ACTIVITY_CLEAR_TASK
andFLAG_ACTIVITY_NEW_TASK
flags.Intent intent = new Intent(SecondActivity.this, CloseActivity.class); //Clear all activities and start new task intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
onCreate()
method ofCloseActivity
activity.@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); finish(); // Exit }
你应该使用
FLAG_ACTIVITY_CLEAR_TASK
和FLAG_ACTIVITY_NEW_TASK
标志。Intent intent = new Intent(SecondActivity.this, CloseActivity.class); //Clear all activities and start new task intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
onCreate()
CloseActivity
活动方法。@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); finish(); // Exit }
回答by Hasib Akter
Use finishAffinity()
method that will finish the current activity and all parent activities. But it works only for API 16+
mean Android 4.1 or higher.
使用finishAffinity()
将完成当前活动和所有父活动的方法。但它仅适用于API 16+
平均 Android 4.1 或更高版本。
API 16+ use:
API 16+ 使用:
finishAffinity();
Below API 16 use:
API 16 以下使用:
ActivityCompat.finishAffinity(this); //with v4 support library
To exit whole app:
退出整个应用程序:
finishAffinity(); // Close all activites
System.exit(0); // Releasing resources
回答by Avnish Choudhary
To clear all the activities while opening new one then do the following:
要在打开新活动时清除所有活动,请执行以下操作:
Intent intent = new Intent(getApplicationContext(), YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
回答by AJay
Sometime finish()
not working
有时finish()
不工作
I have solved that issue with
我已经解决了这个问题
finishAffinity()
finishAffinity()
Do not use
不使用
System.exit(0);
It will finish app without annimation.
它将在没有动画的情况下完成应用程序。
回答by Joel
Hi if you are in a fragment and are not able to use the finish method as it is(because finish should solve your problem) then you can use the getActivity.finish()
method after startActivity(intent);
.
If you are not in a fragment you could directly use finish()
after you startActivity(intent);
line
嗨,如果您在片段中并且无法按原样使用完成方法(因为完成应该可以解决您的问题),那么您可以getActivity.finish()
在startActivity(intent);
. 如果您不在片段中,则可以在行finish()
后直接使用startActivity(intent);
回答by Ankur Kumar
You can try starting the Screen 3 with Intent.FLAG_ACTIVITY_CLEAR_TASK http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
您可以尝试使用 Intent.FLAG_ACTIVITY_CLEAR_TASK http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK启动 Screen 3
回答by Siddharth_Vyas
Add android:noHistory="true"
in your activity manifest file.
添加android:noHistory="true"
您的活动清单文件。
回答by Mitul Gedeeya
There are 2 ways for solve your problem
有2种方法可以解决您的问题
1) call finish() after startActivity(intent) in every activity
1) 在每个活动的 startActivity(intent) 之后调用 finish()
2) set android:launchMode="singleInstance" in every tag in menifest file
2)在menifest文件的每个标签中设置android:launchMode="singleInstance"
i think 2nd way is best for solving problem but you can also use first way
我认为第二种方式最适合解决问题,但您也可以使用第一种方式