6

当用户在我的应用程序中点击“注销”时,我希望他们被带到“登录”活动并终止我的应用程序中所有其他正在运行或暂停的活动。

如果用户以前登录过,我的应用程序正在使用共享首选项绕过启动时的“登录”活动。因此,FLAG_ACTIVITY_CLEAR_TOP 在这种情况下将不起作用,因为当用户被带到那里时,登录活动将位于活动堆栈的顶部。

4

3 回答 3

12

您可以使用 BroadcastReceiver 在您的其他活动中侦听“终止信号”

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

在您的活动中,您注册了一个 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);

然后,您只需从应用程序中的任何位置发送广播

Intent intent = new Intent("CLOSE_ALL");
this.sendBroadcast(intent);
于 2012-09-04T15:22:52.043 回答
8

对于 API 11+,您可以Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK这样使用:

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

它将完全清除所有以前的活动并开始新的活动。

于 2015-02-24T19:05:49.757 回答
2

而不是FLAG_ACTIVITY_CLEAR_TOP使用FLAG_ACTIVITY_CLEAR_TASK(尽管 API 11+):

如果在传递给 Context.startActivity() 的 Intent 中设置,则此标志将导致与该活动相关联的任何现有任务在活动启动之前被清除。也就是说,该活动成为一个空任务的新根,并且所有旧活动都已完成。这只能与 FLAG_ACTIVITY_NEW_TASK 结合使用。

于 2012-09-04T17:48:35.163 回答