如何清除在Android应用程序的所有活动 [英] How to clear all activities in Android app

查看:70
本文介绍了如何清除在Android应用程序的所有活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很多活动可以以任何顺序被称为

My app has many activities that can be called in any order

例活动历史记录:
A - > B - >Ç - >ð - > A - > B - >电子

Example Activity History: A -> B -> C -> D -> A -> B -> E

现在在活动E,I AM'注销'设备(注销用户,并删除他们可能已经下载到他们的SD卡的数据)。欲望的行为是应用重新开始,用户会被提示登录活动,回击返回用户到主屏幕。

Now in activity E, I am 'deregistering' the device (logging the user out, and deleting any data they might have downloaded to their sdcard). The desire behavior is that the app 'starts over' and the user is prompted with a login activity and hitting back will return the user to the home screen.

所以,现在,动鄂应明确以某种方式活动堆栈。目前,发射距离E.问题是,当用户访问了A,然后去了中间活动,才去到E重新审视A A的意图,当我设置FLAG_ACTIVITY_CLEAR_TOP,还有活动在堆栈中。

So now, activity E should clear the activity stack in some way. Currently, I am setting FLAG_ACTIVITY_CLEAR_TOP when launching A's intent from E. The problem is, when the user had visited A and then gone to intermediate activities and revisited A before going to E, there are still activities on the stack.

A - > B - >Ç - >ð - > A

A -> B -> C -> D -> A

因此​​,用户已被注销,不能使用活动B-D,但如果用户点击从活动一回,他们可以访问活动,B-D。有没有一种简单的方法来都较登录活动以外的活动从堆栈中清除?

So the user has been logged out and can't use activities B-D, but if the user hits back from activity A, they can access activities B-D. Is there a simple way to have all the activities other than the login activity be cleared from the stack?

更新:

于是我试着更新我的BaseActivity(每个活动我的应用程序子类中的一个),包含静态标志isDeregistering,告诉活动如果为真,以自我毁灭。问题是,每次活动结束调用(),我也得到引导至主屏幕,并无法重新启动应用程序,直到强制关闭应用程序。有没有更好的方式来做到这一点?

So I've tried updating my BaseActivity (each activity in my app subclasses this one) to contain a static flag isDeregistering that tells the activity to destroy itself if true. The problem is, every activity calls finish(), and I get booted to the homescreen and cannot restart the app until force closing the app. Is there a better way to do this?

推荐答案

我已经想通了一个可行的解决方案。下面code进入我的BaseActivity类。

I've figured out a workable solution. The following code goes into my BaseActivity class.

    public boolean killIfDeregistering() {
    if (isDeregistering) {
        Log.d(TAG, "deregistering true!");
        if (getClass().getName().equals(LoginActivity.class.getName())) {
            //don't destroy activity, reset flag
            Log.d(TAG, "stopping deregister process!");
            isDeregistering = false;
        } else {
            //finish the activity
            Log.d(TAG, "killing this activity!");
            finish();
            return true;
        }
    }
    return false;
}

使用有点反思,我可以决定是否要杀死基地的活动,使家庭发射器可以重新启动在 LoginActivity 应用程序。我只是要确保该LoginActivity不会留在堆栈中它已经通过手动调用完成()就可以进行登录后。

Using a bit of Reflection, I can decide whether or not to kill the base activity, so that the home launcher can restart the app at the LoginActivity. I just have to make sure that the LoginActivity does not remain in the stack after it has performed the login by calling finish() on it manually.

这篇关于如何清除在Android应用程序的所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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