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

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

问题描述

我的应用程序中的所有活动都需要登录后才能查看.用户几乎可以退出任何活动.这是应用程序的要求.无论何时用户注销,我都希望将用户发送到登录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来打开Login活动,这似乎按照文档中的说明进行操作,但是并没有实现我将Login活动置于历史记录底部的目标堆栈,并防止用户导航回以前看到的登录活动.我还尝试对清单中的Login活动使用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并在所有Actvities中安装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);

接收者(安全活动):

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天全站免登陆