Firebase: How to keep an Android user logged in?

前端 未结 4 1666
猫巷女王i
猫巷女王i 2021-02-05 11:53

I\'m using Firebase SimpleLogin to enable Email / Password authentication. Creation of users and subsequent login is all working fine. However, whenever I leave the app (even if

4条回答
  •  滥情空心
    2021-02-05 12:50

    Another way - try this code in your onCreate:

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        // User is signed in
        Intent i = new Intent(LoginActivity.this, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(i);
    } else {
        // User is signed out
        Log.d(TAG, "onAuthStateChanged:signed_out");
    }
    

    This will keep the user logged in by taking the user to the Main activity directly without stopping at registration activity. so the user will be logged in unless the user click on signout.

提交回复
热议问题