在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中加入代码

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

注意 A activity必须是单列的

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

更多相关文章

  1. Android软件广告屏蔽方法及代码
  2. IPC机制: Android中的IPC简介和多进程模式
  3. Android studio使用技巧(二:国际化以及代码重构)
  4. Android的反编译和代码混淆
  5. 在Android上实现HttpServer的示例代码
  6. Android WebView 调用React Js 代码
  7. android recover 系统代码分析 -- 选择进入

随机推荐

  1. Android缺少awk:安装busybox
  2. Android(安卓)开发之多线程处理、Handler
  3. Android(安卓)Studio NDK开发案例一 JNI
  4. Android FileObserver 用法
  5. Android Binder------ServiceManager启动
  6. Android root file system 的创建
  7. android 的内存、内部存储和外部存储的理
  8. Android Studio 使用Lambda表达式
  9. NDK/JNI02-实例开发流程
  10. 关于设置activity样式