简体   繁体   English

检查活动是否来自其他活动

[英]Check whether an activity is active or not from a different activity

I have a flow in my application like this: 我的应用程序中有这样一个流程:

For new users: 对于新用户:

Splash Screen --> Login Activity --> Home Activity 启动画面->登录活动->主页活动

For already registered users: 对于已经注册的用户:

Splash Screen --> Home Activity 启动画面->家庭活动

Basically the Splash Screen has an if else to decide which activity to go to. 基本上, 启动画面有一个if来决定要进行的活动。 Once a first time user logs in, his status is saved in a preference variable for the splash screen to decide next time not to open the login activity. 首次用户登录后,其状态将保存在初始屏幕的首选项变量中,以决定下次不打开登录活动。

Now the situation is that. 现在的情况就是这样。 If a new user logs in and goes to the home activity, and then logs out. 如果有新用户登录并进入家庭活动,然后注销。 He is redirected to the Login screen which is pretty much what should happen. 他被重定向到登录屏幕,这几乎应该发生。 But, in case an existing user opens the app, he is shown the Splash screen and directly moved to the Home Activity. 但是,如果现有用户打开该应用程序,则会显示“启动画面”并直接将其移至“家庭活动”。 Now if the user logs out, he gets out of the app. 现在,如果用户注销,他将退出该应用程序。 This happens because the Login Activity does not have any instance created and thus finishing the Home Activity finishes the whole app. 发生这种情况是因为Login Activity没有创建任何实例,因此完成Home Activity将完成整个应用程序。 Logout actually finishes the Home Activity, naturally the last active activity should open up. 注销实际上完成了家庭活动,自然应该打开最后一个活动。 Which is not happening. 这没有发生。

What I want to do is that, I want to implement a logic which will check that the Login Activity is available or not. 我想做的是,我想实现一个逻辑,该逻辑将检查Login Activity是否可用。 If its available then finish() will be called else the Login Activity will be called via intent. 如果可用,则将调用finish()否则将通过intent调用Login Activity。

Please tell me how to achieve this. 请告诉我如何实现这一目标。

PS: My app uses a custom theme with a customized action bar. PS:我的应用程序使用带有自定义操作栏的自定义主题。 If I call finish and Intent together or I use flags to clear existing activities then there is a weird transition effect which shows the black standard action bar for a split second thus creating a bad user experience. 如果我同时调用finish和Intent或使用标志来清除现有活动,则会有一个奇怪的过渡效果,该效果在瞬间显示黑色标准操作栏,从而带来不良的用户体验。

Now if the user logs out, he gets out of the app. 现在,如果用户注销,他将退出该应用程序。 This happens because the Login Activity does not have any instance created and thus finishing the Home Activity finishes the whole app. 发生这种情况是因为Login Activity没有创建任何实例,因此完成Home Activity将完成整个应用程序。

If i understood your question, why dont you just call the Login Activity manually after user click a logout button? 如果我理解您的问题,为什么您不只是在用户单击logout按钮之后手动调用Login Activity

Its what i always did with apps that have flow like yours 这是我一直对像您这样的流量应用程序所做的事情

when user login finish login activity and start home activity. 当用户登录完成登录活动并开始家庭活动时。 when user logout finish home activity and start login activity 用户注销完成家庭活动并开始登录活动时

You always can call Login Activity via intent. 您始终可以通过意图调用“登录活动”。 If the activity is available, android will show this activity. 如果活动可用,则android将显示此活动。 Else android will create new activity automatically. 否则,android将自动创建新活动。 Actually that's why we use intents to show activity instead of creating activityes manually. 实际上,这就是为什么我们使用意图显示活动而不是手动创建活动的原因。 System catches this intents and does all dirty work. 系统抓住了这个意图,做了所有肮脏的工作。

EDIT: 编辑:

Hmm, but wouldn't you have the transition problem anyways? 嗯,但是您还是不会遇到转换问题吗? (If you were already logged in, and then log out - using intent/finish() you will have the same black action bar issue no?) (如果您已经登录,然后注销-使用intent / finish()您是否会遇到相同的黑色操作栏问题?)

Maybe consider following ( I actually did this in my app): 也许考虑以下(我实际上在我的应用程序中做了此操作):

Merge Splash screen and Login into one activity and depending whether you are logged in - display the login fields or proceed to home screen. 将“启动画面”和“登录”合并到一项活动中,并取决于您是否已登录-显示登录字段或进入主屏幕。 Then you have a out of box consistent stack of activities regardless of use cases and no mambo-jumbo with do I already have this or not. 然后,无论使用案例如何,您都可以得到一系列一致的活动,而且没有mambo-jumbo,我是否已经拥有此功能。


I can't comment because of I lack 4 rep, so I'll post as answer here: 由于缺少4个代表,因此我无法发表评论,因此我将在此处发布答案:

I think @Blaze Tama is right. 我认为@Blaze Tama是正确的。 You can also use FLAG_ACTIVITY_CLEAR_TOP on intent to avoid stack flow problems: 您也可以出于意图使用FLAG_ACTIVITY_CLEAR_TOP来避免堆栈流问题:

From docs: 从文档:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. 如果已设置,并且正在启动的活动已在当前任务中运行,那么与其启动该活动的新实例,不如关闭该活动之上的所有其他活动,并将此Intent传递给(现在顶部)将旧活动作为新的Intent。

Always start Login activity and start Home activity right away if the user already logged in. 如果用户已经登录,请始终立即启动“登录”活动并立即启动“主页”活动。

In the Splash Screen activity 在启动画面活动中

Intent intent = new Intent(this, Login.class);
If (user already logged in)
{
    intent.putextra("Logged in", true);
}

startActivity(intent); 

In the Login activity 在登录活动中

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    if (intent != null)
    {
        if (intent.getBooleanExtra("Logged in", false))
        {
            startActivityForResult(new Intent(this, Home.class), requestCode);
        }
    }
    else
    {
        // The existing code here
    }
}

In Home activity send back a code to indicate if the user logged out or just BackPress. 在“家庭活动”中,发送回代码以指示用户是注销还是仅BackPress。 If BackPress finish this Login activity. 如果BackPress完成此登录活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM