Check whether an activity is active or not from a different activity

333 views Asked by At

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. 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. 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. If its available then finish() will be called else the Login Activity will be called via intent.

Please tell me how to achieve this.

P.S: My app uses a custom theme with a customized action bar. 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.

5

There are 5 answers

7
Ravi On BEST ANSWER

when user login finish login activity and start home activity. when user logout finish home activity and start login activity

1
Nemanja Maksimovic On

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?)

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.


I can't comment because of I lack 4 rep, so I'll post as answer here:

I think @Blaze Tama is right. You can also use FLAG_ACTIVITY_CLEAR_TOP on intent to avoid stack flow problems:

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.

6
Blaze Tama On

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.

If i understood your question, why dont you just call the Login Activity manually after user click a logout button?

Its what i always did with apps that have flow like yours

0
Hoan Nguyen On

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. If BackPress finish this Login activity.

6
user3390963 On

You always can call Login Activity via intent. If the activity is available, android will show this activity. Else android will create new activity automatically. Actually that's why we use intents to show activity instead of creating activityes manually. System catches this intents and does all dirty work.