Properly skip login activity if already logged in

安稳与你 提交于 2019-12-02 17:41:28

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

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);
            }
        });
    }
}
user2784855

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));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!