Android的 - 杀死注销所有活动Android

2023-09-05 04:50:26 作者:放荡不羁的狗子

当用户点击注销在我的申请,我希望他们能够在我的应用程序带到登录活动,并杀死所有正在运行或暂停活动。

When a user taps "Logout" within my application, I would like them to be taken to the "Login" activity and kill all other running or paused activities within my application.

我的应用程序正在使用的共享preferences可以绕过推出的登录活动,如果用户已登录previously。因此,FLAG_ACTIVITY_CLEAR_TOP不会在这种情况下工作,因为登录活动将在活动堆栈的顶部,当用户拍摄有

My application is using Shared Preferences to bypass the "Login" activity on launch if the user has logged in previously. Therefore, FLAG_ACTIVITY_CLEAR_TOP will not work in this case, because the Login activity will be at the top of the activity stack when the user is taken there.

推荐答案

您可以使用一个BroadcastReceiver监听你的其他活动杀信号

You could use a BroadcastReceiver to listen for a "kill signal" in your other activities

http://developer.android.com/reference/android/content/BroadcastReceiver.html

在你的活动,你注册一个BroadcastReceiver

In your Activities you register a BroadcastReceiver

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("CLOSE_ALL");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // close activity
  }
};
registerReceiver(broadcastReceiver, intentFilter);

然后你只需发送一个广播,随时随地在你的应用程序

Then you just send a broadcast from anywhere in your app

Intent intent = new Intent("CLOSE_ALL");
this.sendBroadcast(intent);