1. RxBus优雅式

首先,在基类BaseActivity里,注册RxBus监听:

  1. public class BaseActivity3 extends AppCompatActivity {
  2. Subscription mSubscription;
  3. @Override
  4. public void onCreate(@Nullable Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. initRxBus();
  7. }
  8. //接收退出的指令,关闭所有activity
  9. private void initRxBus() {
  10. mSubscription = RxBus.getInstance().toObserverable(NormalEvent.class)
  11. .subscribe(new Action1<NormalEvent>() {
  12. @Override
  13. public void call(NormalEvent userEvent) {
  14. if (userEvent.getType() == -1) {
  15. finish();
  16. }
  17. }
  18. },
  19. new Action1<Throwable>() {
  20. @Override
  21. public void call(Throwable throwable) {
  22. }
  23. });
  24. }
  25. @Override
  26. protected void onDestroy() {
  27. super.onDestroy();
  28. if (!mSubscription.isUnsubscribed()) {
  29. mSubscription.unsubscribe();
  30. }
  31. }
  32. }

这是事件实体NormalEvent:

  1. public class NormalEvent {
  2. private int type;
  3. public NormalEvent(int type) {
  4. this.type = type;
  5. }
  6. public int getType() {
  7. return type;
  8. }
  9. public void setType(int type) {
  10. this.type = type;
  11. }
  12. }

新建RxBus类

  1. public class RxBus {
  2. private static volatile RxBus mInstance;
  3. private final Subject bus;
  4. public RxBus()
  5. {
  6. bus = new SerializedSubject<>(PublishSubject.create());
  7. }
  8. /**
  9. * 单例模式RxBus
  10. *
  11. * @return
  12. */
  13. public static RxBus getInstance()
  14. {
  15. RxBus rxBus2 = mInstance;
  16. if (mInstance == null)
  17. {
  18. synchronized (RxBus.class)
  19. {
  20. rxBus2 = mInstance;
  21. if (mInstance == null)
  22. {
  23. rxBus2 = new RxBus();
  24. mInstance = rxBus2;
  25. }
  26. }
  27. }
  28. return rxBus2;
  29. }
  30. /**
  31. * 发送消息
  32. *
  33. * @param object
  34. */
  35. public void post(Object object)
  36. {
  37. bus.onNext(object);
  38. }
  39. /**
  40. * 接收消息
  41. *
  42. * @param eventType
  43. * @param <T>
  44. * @return
  45. */
  46. public <T> Observable<T> toObserverable(Class<T> eventType)
  47. {
  48. return bus.ofType(eventType);
  49. }
  50. }

最后,在需要退出的地方调用:

  1. RxBus.getInstance().post(new NormalEvent(-1));//发送退出指令

2. 容器式:

建立一个全局容器,把所有的Activity存储起来,退出时循环遍历finish所有Activity

  1. public class BaseActivity extends AppCompatActivity {
  2. @Override
  3. public void onCreate(@Nullable Bundle savedInstanceState ) {
  4. super.onCreate(savedInstanceState);
  5. ActivityManager.getActivityManager().addActivity(this);
  6. }
  7. @Override protected void onDestroy() {
  8. super.onDestroy();
  9. // 结束Activity&从栈中移除该Activity
  10. ActivityManager.getActivityManager().finishActivity();
  11. }
  12. }
  13. public class ActivityManager {
  14. // Activity栈
  15. private static Stack<Activity> activityStack;
  16. // 单例模式
  17. private static ActivityManager instance;
  18. private ActivityManager() {
  19. }
  20. /**
  21. * 单一实例
  22. */
  23. public static ActivityManager getActivityManager() {
  24. if (instance == null) {
  25. instance = new ActivityManager();
  26. }
  27. return instance;
  28. }
  29. /**
  30. * 添加Activity到堆栈
  31. */
  32. public void addActivity(Activity activity) {
  33. if (activityStack == null) {
  34. activityStack = new Stack<Activity>();
  35. }
  36. activityStack.add(activity);
  37. }
  38. /**
  39. * 获取当前Activity(堆栈中最后一个压入的)
  40. */
  41. public Activity currentActivity() {
  42. Activity activity = activityStack.lastElement();
  43. return activity;
  44. }
  45. /**
  46. * 结束当前Activity(堆栈中最后一个压入的)
  47. */
  48. public void finishActivity() {
  49. Activity activity = activityStack.lastElement();
  50. finishActivity(activity);
  51. }
  52. /**
  53. * 结束指定的Activity
  54. */
  55. public void finishActivity(Activity activity) {
  56. if (activity != null) {
  57. activityStack.remove(activity);
  58. activity.finish();
  59. activity = null;
  60. }
  61. }
  62. /**
  63. * 结束指定类名的Activity
  64. */
  65. public void finishActivity(Class<?> cls) {
  66. for (Activity activity : activityStack) {
  67. if (activity.getClass().equals(cls)) {
  68. finishActivity(activity);
  69. }
  70. }
  71. }
  72. /**
  73. * 结束所有Activity
  74. */
  75. public void finishAllActivity() {
  76. for (int i = 0; i < activityStack.size(); i++) {
  77. if (null != activityStack.get(i)) {
  78. activityStack.get(i).finish();
  79. }
  80. }
  81. activityStack.clear();
  82. }
  83. /**
  84. * 退出应用程序
  85. */
  86. public void AppExit(Context context) {
  87. try {
  88. finishAllActivity();
  89. //根据进程ID,杀死该进程
  90. android.os.Process.killProcess(android.os.Process.myPid());
  91. //退出真个应用程序
  92. System.exit(0);
  93. } catch (Exception e) {
  94. }
  95. }
  96. }

3. 广播式

通过在BaseActivity中注册一个广播,当退出时发送一个广播,finish退出

  1. public class BaseActivity2 extends AppCompatActivity {
  2. private static final String EXITACTION = "action.exit";
  3. private ExitReceiver exitReceiver = new ExitReceiver();
  4. @Override protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. IntentFilter filter = new IntentFilter();
  7. filter.addAction(EXITACTION);
  8. registerReceiver(exitReceiver, filter);
  9. }
  10. @Override protected void onDestroy() {
  11. super.onDestroy(); unregisterReceiver(exitReceiver);
  12. }
  13. class ExitReceiver extends BroadcastReceiver {
  14. @Override public void onReceive(Context context, Intent intent) {
  15. BaseActivity2.this.finish();
  16. }
  17. }
  18. }

4. SingleTask

1、设置MainActivity的加载模式为singleTask

  1. android:launchMode="singleTask"

2、将退出出口放置在MainActivity

  1. private boolean mIsExit;
  2. @Override /** * 双击返回键退出 */
  3. public boolean onKeyDown(int keyCode, KeyEvent event) {
  4. if (keyCode == KeyEvent.KEYCODE_BACK) {
  5. if (mIsExit) {
  6. this.finish();
  7. } else {
  8. Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
  9. mIsExit = true;
  10. new Handler().postDelayed(new Runnable() {
  11. @Override public void run() {
  12. mIsExit = false;
  13. }
  14. }, 2000);
  15. } return true;
  16. } return super.onKeyDown(keyCode, event);
  17. }

5. SingleTask改版式

第一步设置MainActivity的加载模式为singleTask

  1. android:launchMode="singleTask"

第二步重写onNewIntent()方法

  1. private static final String TAG_EXIT = "exit";
  2. @Override
  3. protected void onNewIntent(Intent intent) {
  4. super.onNewIntent(intent);
  5. if (intent != null) {
  6. boolean isExit = intent.getBooleanExtra(TAG_EXIT, false);
  7. if (isExit) { this.finish();
  8. }
  9. }
  10. }

第三步 退出

  1. Intent intent = new Intent(this,MainActivity.class); intent.putExtra(MainActivity.TAG_EXIT, true);
  2. startActivity(intent);