ANDROID 应用退出

在android中finish()和system(0)都只能退出单个activity。杀进程等的等方式都不行~~~
解决问题:

在针对 多activity中退出整个程序,例如从A->B->C->D,这时我需要从D直接退出程序。


我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在D窗口打开A窗口时在Intent中直接加入标志Intent.FLAG_ACTIVITY_CLEAR_TOP,再次开启A时将会清除该进程空间的所有Activity。
在D中使用下面的代码:

Intent intent = new Intent(); 
intent.setClass(D.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置
startActivity(intent);
finish();

然后在A中加入代码

Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
//退出
 if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & intent.getFlags()) != 0) {
 finish();
 }
}

注意 A activity必须是单列的 

因此A的Manifest.xml配置成android:launchMode="singleTop"

原文地址:https://www.cnblogs.com/xiezhengcai/p/3472462.html