Question

My launcher icon currently starts the login activity. I've stored the logged in status in SharedPreferences. Is there any way to properly skip the login activity and go straight to the main activity without any UI glitches. All existing solutions involving finish() in onCreate() cause the login activity title to be briefly visible or some other brief blank screen UI glitch.

Was it helpful?

Solution

Have a launcher acitivy with no UI that decides to open the MainActivity or the LoginActivity. You can declare no UI with:

android:theme="@android:style/Theme.NoDisplay"

Two other possible solutions:

Just do it the other way around: make your mainActivity your launcher and make it check whether the user is logged in. Then redirect to the loginActivity when this is not the case.

Another way is to work with fragments. Have a base activity that can load both the mainFragment and the loginFragment. For reference: https://developer.android.com/training/basics/fragments/index.html

OTHER TIPS

You can create a Base Activity that will check if the user's username and password is already in the SharedPreferences and starts the activity if it exist hence not.

example:

public class BeanStalkBaseActivity extends SherlockActivity{

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

    setContentView(R.layout.activity_main);


    if(SavedPreference.getUserName(this).length() == 0)
    {
        Intent intent = new Intent(this,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
    }else
    {
        Intent intent = new Intent(this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
    }

}

}

BeanStalkBaseActivity should be your Launcher as it only serve as a checker.

You can also check for login status during your splash screen activity if you have one. Splash screens are great for letting users know the app hasn't stalled when it's loading and can also be used to redirect the app to the appropriate screen.

I followed this great guide my first time making one: https://www.bignerdranch.com/blog/splash-screens-the-right-way/

If you check whether the user is already logged in or not inside the main activity or the current activity and then switch to another activity if logged in, this will lead to UI glitches, i.e. your current activity will show up for a second or half and then it will switch to the target activity.

You can do this like :

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAuth = FirebaseAuth.getInstance();
    if (mAuth.getCurrentUser() != null) {

        Toast.makeText(MainActivity.this, "Already Logged In", 
        Toast.LENGTH_LONG).show();
        Intent intent = new Intent(MainActivity.this, Home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

    } else {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        BtnSignUp = findViewById(R.id.btnSignUp);
        BtnLogIn = findViewById(R.id.btnLogIn);


        BtnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent signUp = new Intent(MainActivity.this, SignUpActivity.class);
                startActivity(signUp);

            }
        });

        BtnLogIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent logIn = new Intent(MainActivity.this, Login.class);
                startActivity(logIn);
            }
        });
    }
}

In main activity just check if user is not null then fire up home

firebaseAuth = FirebaseAuth.getInstance();

FirebaseUser user = firebaseAuth.getCurrentUser();

if (user != null) {
    finish();
    startActivity(new Intent(MainActivity.this, UserHomeActivity.class));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top