注销时,清除活动历史堆栈,防止“返回"用于打开仅登录活动的按钮 [英] On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activities

查看:26
本文介绍了注销时,清除活动历史堆栈,防止“返回"用于打开仅登录活动的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的所有活动都需要用户登录才能查看.用户几乎可以从任何活动中注销.这是应用程序的要求.在任何时候,如果用户注销,我想将用户发送到登录 Activity.在这一点上,我希望此活动位于历史堆栈的底部,以便按后退"按钮将用户返回到 Android 的主屏幕.

All activities in my application require a user to be logged-in to view. Users can log out from almost any activity. This is a requirement of the application. At any point if the user logs-out, I want to send the user to the Login Activity. At this point I want this activity to be at the bottom of the history stack so that pressing the "back" button returns the user to Android's home screen.

我在几个不同的地方看到过这个问题,所有答案都相似(我在此处概述),但我想在这里提出它以收集反馈.

I've seen this question asked a few different places, all answered with similar answers (that I outline here), but I want to pose it here to collect feedback.

我尝试通过将其 Intent 标志设置为 FLAG_ACTIVITY_CLEAR_TOP 来打开登录活动,这似乎按照文档中的说明进行,但没有实现我的目标将登录活动放在历史堆栈的底部,并防止用户导航回以前看到的登录活动.我还尝试将 android:launchMode="singleTop" 用于清单中的登录活动,但这也没有实现我的目标(而且似乎没有任何效果).

I've tried opening the Login activity by setting its Intent flags to FLAG_ACTIVITY_CLEAR_TOP which seems to do as is outlined in the documentation, but does not achieve my goal of placing the Login activity at the bottom of the history stack, and preventing the user from navigating back to previously-seen logged-in activities. I also tried using android:launchMode="singleTop" for the Login activity in the manifest, but this does not accomplish my goal either (and seems to have no effect anyway).

我认为我需要清除历史堆栈,或者完成所有以前打开的活动.

I believe I need to either clear the history stack, or finish all previously- opened activities.

一种选择是让每个活动的 onCreate 检查登录状态,如果未登录,则 finish().我不喜欢这个选项,因为后退按钮仍然可以使用,当活动关闭时导航回来.

One option is to have each activity's onCreate check logged-in status, and finish() if not logged-in. I do not like this option, as the back button will still be available for use, navigating back as activities close themselves.

下一个选项是维护一个 LinkedList 对所有开放活动的引用,这些活动可以从任何地方静态访问(可能使用弱引用).在注销时,我将访问此列表并遍历所有先前打开的活动,对每个活动调用 finish().我可能很快就会开始实施这种方法.

The next option is to maintain a LinkedList of references to all open activities that is statically accessible from everywhere (perhaps using weak references). On logout I will access this list and iterate over all previously-opened activities, invoking finish() on each one. I'll probably begin implementing this method soon.

不过,我宁愿使用一些 Intent 标记技巧来完成此操作.我非常高兴地发现我可以满足我的应用程序的要求,而无需使用我上面概述的两种方法中的任何一种.

I'd rather use some Intent flag trickery to accomplish this, however. I'd be beyond happy to find that I can fulfill my application's requirements without having to use either of the two methods that I've outlined above.

有没有办法通过使用 Intent 或清单设置来实现这一点,或者是我的第二个选择,维护一个打开活动的 LinkedList 是最好的选择?或者还有其他我完全忽略的选择吗?

Is there a way to accomplish this by using Intent or manifest settings, or is my second option, maintaining a LinkedList of opened activities the best option? Or is there another option that I'm completely overlooking?

推荐答案

我可以建议你另一种更强大的方法.基本上,您需要向所有需要保持登录状态的活动广播注销消息.因此,您可以使用 sendBroadcast 并在所有活动中安装 BroadcastReceiver.像这样:

I can suggest you another approach IMHO more robust. Basically you need to broadcast a logout message to all your Activities needing to stay under a logged-in status. So you can use the sendBroadcast and install a BroadcastReceiver in all your Actvities. Something like this:

/** on your logout method:**/
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);

接收者(受保护的活动):

The receiver (secured Activity):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /**snip **/
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.package.ACTION_LOGOUT");
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("onReceive","Logout in progress");
            //At this point you should start the login activity and finish this one
            finish();
        }
    }, intentFilter);
    //** snip **//
}

这篇关于注销时,清除活动历史堆栈,防止“返回"用于打开仅登录活动的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆