0

I know this question has been asked so many times on SO, so take a moment before you down vote it. 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.

In my application I'm storing the user login details in SharedPreferences. Basically when my application starts - Activity A - Splash screen will show up from 3 seconds. Then it will go to Activity B - The user login page, on press of login the credentials are stored in SharedPreference. Then it will go to Activity C - Home page. 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.

So the flow is like this,

If SharedPreference is empty

A->B->C

If values are present in 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. 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. Even after this if I open it from launcher it goes to login page i.e Activity B.

I'm overriding the back button like this on Activity 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? Q2) If launched from recent list, why it is not redirecting to Activity C even when SharedPreference has values?

Sorry to ask very lengthy question, I know it is not appropriate. Thank you

Community
  • 1
  • 1
Aniruddha
  • 4,477
  • 2
  • 21
  • 39

1 Answers1

3

i will put my thoughts...

  1. why r u checking sharedpreference in B at all? if u have stored in sharedprefernce redirect the user directly to C from A .

    if(sharedPreference) -> C

    else 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.

  3. if u dont need the activity to exist u can call finish() and also put noHistory='true' in manifest...

plz see to these points and rectify me if i m wrong anywhere...

thx and hope it helps u...

Ahmad
  • 437
  • 1
  • 4
  • 12
  • I can see activity A only when I run it for the first time and I logout. If values are present in SharedPreferences it is not launching. – Aniruddha May 08 '14 at 10:04
  • See #2 of this answer. You can close C and start A from there. It can be a problem though if screen orientation changed - you have no way of knowing if OnResume was called by that or by launch from recents. Oh, and BTW: "launch" from recents is actually only bringing the app back to front, it was never killed in the first place. You can use #3 in OnPause if you want to kill it. – velis May 08 '14 at 10:30