简体   繁体   English

Android:关闭应用程序并从最近的列表中打开它

[英]Android: Closing the application and opening it from recent list

I know this question has been asked so many times on SO, so take a moment before you down vote it. 我知道这个问题在SO上已经问了很多遍了,所以花点时间在您否决它之前。 I have gone through How to remove application from recent application list? 我已经经历过如何从最近的应用程序列表中删除应用程序?

Quitting an application - is that frowned upon? 退出申请-对此感到不满意吗?

Close application and remove from recent apps 关闭应用程序并从最近的应用程序中删除

And many other questions on SO. 以及关于SO的许多其他问题。

In my application I'm storing the user login details in SharedPreferences. 在我的应用程序中,我将用户登录详细信息存储在SharedPreferences中。 Basically when my application starts - Activity A - Splash screen will show up from 3 seconds. 基本上,当我的应用程序启动时-活动A-启动屏幕将在3秒后显示。 Then it will go to Activity B - The user login page, on press of login the credentials are stored in SharedPreference. 然后将转到活动B-用户登录页面,按登录后,凭据将存储在SharedPreference中。 Then it will go to Activity C - Home page. 然后它将转到活动C-主页。 I don't want the user to enter the login details each time so I'm storing them in SharedPreference, if the value is present then redirect directly to Activity C - Home Page. 我不希望用户每次输入登录详细信息,因此我将其存储在SharedPreference中,如果存在该值,则直接重定向到“活动C-主页”。

So the flow is like this, 所以流程是这样的,

If SharedPreference is empty 如果SharedPreference为空

A->B->C

If values are present in SharedPreference 如果SharedPreference中存在值

A->B->C, I'm checking in Activity B and when the values are present redirect it to C without making the B to come to front.

This all works fine if I open application from launcher. 如果我从启动器中打开应用程序,则一切正常。 But if I open it from recent list even though the values are present in SharedPreference it is going to Activity B. And when I open it from launcher or recent list the Activity A (splash screen) doesn't show up. 但是,即使我在最近列表中打开它,即使SharedPreference中存在值,它也会转到活动B。当我从启动器或最近列表中打开它时,活动A(初始屏幕)不会显示。 Only if I logout and open the application it shows up. 仅当我注销并打开应用程序时,它才会显示。 What would be the problem? 有什么问题吗?

I tired putting <activity android:excludeFromRecents="true"> so forcing it to not to appear in recent list. 我厌倦了将<activity android:excludeFromRecents="true">放入,以免它不出现在最近的列表中。 Even after this if I open it from launcher it goes to login page ie Activity B. 即使在此之后,如果我从启动器中打开它,它也会进入登录页面,即活动B。

I'm overriding the back button like this on Activity C 我覆盖了活动C上的后退按钮

public void onBackPressed() {


    if (backPressedOnce) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        super.onBackPressed();
        return;
    }
    this.backPressedOnce = true;

    Toast.makeText(this, "Please press back again to exit.", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            backPressedOnce = false; 

        }
    }, 3000);



} 

public void onResume()
{
    super.onResume();
    this.backPressedOnce = false ;
}

So I have two questions 所以我有两个问题

Q1) How to make splash screen appear each time the user launches it? Q1)如何使启动屏幕在用户每次启动时出现? Q2) If launched from recent list, why it is not redirecting to Activity C even when SharedPreference has values? Q2)如果从最新列表启动,为什么即使SharedPreference具有值,它也不会重定向到活动C?

Sorry to ask very lengthy question, I know it is not appropriate. 很抱歉问一个很长的问题,我知道这是不合适的。 Thank you 谢谢

i will put my thoughts... 我会把我的想法...

  1. why ru checking sharedpreference in B at all? 为什么要检查B中的sharedpreference呢? if u have stored in sharedprefernce redirect the user directly to C from A . 如果您已存储在sharedprefernce中,则将用户从A直接重定向到C。

    if(sharedPreference) -> C if(sharedPreference)-> C

    else B -> C 否则B-> C

  2. when u are launching from recent afaik it will call onresume and not oncreate and also i believe you are checking in oncreate hence its not executing when launching from recent , use logs to know the cycle also please refer to activity life cycle. 当您从最近的afaik启动时,它将调用onresume而不是oncreate,而且我相信您正在签入oncreate,因此从最近启动时它不会执行,请使用日志了解周期,也请参阅活动生命周期。

  3. if u dont need the activity to exist u can call finish() and also put noHistory='true' in manifest... 如果您不需要该活动存在,则可以调用finish()并在清单中放入noHistory ='true'。

plz see to these points and rectify me if im wrong anywhere... 请看这些要点,如果我在任何地方错了,请纠正我...

thx and hope it helps u... 谢谢,希望对您有帮助...

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

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