在注销,明确活动历史堆栈,preventing"返回]无法打开登录的,只有Activites按钮堆栈、无法打开、按钮、明确

2023-09-11 11:20:24 作者:微笑背后隐藏的悲伤。

在我的应用程序的所有活动都需要用户登录后才能查看。用户可以从几乎任何活动注销。这是应用程序的要求。如果在用户登录,任何一点上,我想用户发送到登录活动。在这一点上我想这个活动是在历史堆栈的底部,以便pressing返回按钮,返回用户到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.

我试图通过设置意图标志 FLAG_ACTIVITY_CLEAR_TOP 打开登录活动似乎做的是文档中所概述的,但并没有达到我的放置登录活性历史堆栈的底部的目标,和$ P $从导航回previously见过登录的活动pventing用户。我也尝试过使用安卓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).

我相信我需要或者清除历史堆栈,或者完成所有previously-开业活动。

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

一种选择是让每个活动的的onCreate 检查登录状态,而完成()如果没有登录-在。我不喜欢这个选项,因为后退按钮将仍然可以使用,导航早在活动封闭自己。

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 。在注销我将访问列表和遍历所有previously开放活动,对每项调用完成()。我可能会很快开始实施这一方法。

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.

我宁愿用一些意图标志挂羊头卖狗肉然而,做到这一点。我会超越惊喜地发现,我可以实现我的应用程序的需求,而无需使用,要么我上面提到的两种方法。

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.

有没有办法使用意图或舱单设置来做到这一点,或者是我的第二个选项,维护的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);

接收器(安全活动):

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 **//
}
 
精彩推荐
图片推荐