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

网上资料:{
finish()和system(0)都只能退出单个activity。杀进程等的等方式都不行~~~
解决问题:
我们知道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的Manifest.xml配置成android:launchMode="singleTop"

原理总结:
一般A是程序的入口点,从D起一个A的activity,加入标识Intent.FLAG_ACTIVITY_CLEAR_TOP这个过程中会把栈中B,C,都清理掉。因为A是android:launchMode="singleTop"
不会调用oncreate(),而是响应onNewIntent()这时候判断Intent.FLAG_ACTIVITY_CLEAR_TOP,然后把A finish()掉。
栈中A,B,C,D全部被清理。所以整个程序退出了。

更多相关文章

  1. 一款霸榜 GitHub 的开源 Linux 资源监视器!
  2. Android(安卓)项目上线流程总结
  3. .NET程序员的iOS面试之旅
  4. [Android]BootChart在Android中的应用
  5. Android软件发布--数字签名与优化
  6. AndroidManifest---定义Android清单
  7. Android中的沙箱模型
  8. Android开发——应用程序生命周期
  9. Android属性系统(转载)

随机推荐

  1. Android Bad notification for startFore
  2. Android中执行adb命令
  3. 获取android 刚发出去的短信
  4. Android Support Multidex原理分析
  5. How to compile Android(安卓)2.3 on Ubu
  6. Android获取通讯录联系人,右侧字母滑动索
  7. Android 发送通知 notification
  8. android 调用系统照相机拍照后保存到系统
  9. Android / iOS 招聘
  10. Linux Mint下Kindle Fire调试android程序