1

I have 4 activities:

  • MainActivity (launcher) - checks if user is logged in then start ContainerActivity else IntroActivity
  • IntroActivity - shows various login methods including facebook login
  • LoginActivity - contains phone or email login method. After successful login start ContainerActivity
  • ContainerActivity - main application

I have 2 problems.

First, I want to remove IntroActivity from stack when user successfully login from LoginActivity. Can't use android:noHistory="true" because when user decides to use facebook to login not phone or email, IntroActivity should be in stack.

Second, when user logout from any activity, I want to back to MainActivity and clearing all back stack. I will add activities in future. What is the right way to do this?

    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.Launcher"
        android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".intro.IntroActivity">

        <intent-filter>

            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>
    <activity
        android:name=".authentication.LoginActivity"
        android:parentActivityName=".intro.IntroActivity"
        android:noHistory="true" />
1
  • This is not a Firebase related issue. Please don't add tags that do not correspond with your problem.
    – Alex Mamo
    Commented Nov 15, 2018 at 12:15

2 Answers 2

1

To remove IntroActivity from stack after successful login, I would do this:

Assuming that LoginActivity should also be finished (removed from the stack) on successful login, you can call startActivity() to return to IntroActivity and add an "extra" to the Intent telling IntroActivity to start ContainerActivity and finish itself. Do this:

in LoginActivity:

Intent intent = new Intent(this, IntroActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("startContainerActivity", true);
startActivity(intent);
finish();

in IntroActivity.onNewIntent():

if (intent.hasExtra("startContainerActivity")) {
    Intent launchIntent = new Intent(this, ContainerActivity.class);
    startActivity(launchIntent);
    finish();
}

IntroActivity will then launch ContainerActivity and finish itself, leaving the stack: MainActivity->ContainerActivity.

To return to MainActivity from any other Activity, you just need to override onBackPressed() and return to MainActivity.

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    finish();
}

When you specify the flags CLEAR_TOP and SINGLE_TOP this tells Android to remove all the activities on top of the target Activity and to reuse the target Activity (ie: not create a new instance of the target Activity). In this case, onNewIntent() is called on the existing instance of the Activity.

0

You can start IntoActivity using startActivityForResult and in LoginActivity after successfully loged in, you can set result RESULT_OK and in IntroActivity onActivityResult check for result code and finish IntoActivity.

Not the answer you're looking for? Browse other questions tagged or ask your own question.