Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase getCurrentUser is returning null in new android activity

LoginActivity:

private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    firebaseAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                finish();
                startActivity(new Intent(LoginActivity.this, MainActivity.class));

            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
        }
    };
}

MainActivity:

public  FirebaseAuth firebaseAuth;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    firebaseAuth = FirebaseAuth.getInstance();

    FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();


    if (firebaseUser == null) {
        //finish();
        //startActivity(new Intent(MainActivity.this, LoginActivity.class));
    } else {
        Log.d(TAG, "THE USER IS NOT NULLL. his email is: " +      firebaseUser.getEmail());
   }

    button.setOnClickListener(new OnClick(){
         startActivity(new Intent(ActivityB.class));
    });
}

ActivityB:

private FirebaseAuth firebaseAuth;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_friend);
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    //Here the user is always null
  }

Okay so the scenario that is happening: I'm logging in successfully to the app, in MainActivity.java it's printing that the user is not null. But when I press the button, ActivityB.java is launching but the user and the FirebaseAuth is always null.

I previously added a authListener in MainActivity.java and I figured out that whenever I start a new activity from the MainActivity the listener is triggered and its user would be null.

So why is the listener triggered when I start an Intent, and how do I access the firebaseAuth and currentUser from different activities?

like image 501
riadrifai Avatar asked Jan 08 '17 13:01

riadrifai


1 Answers

I had this same problem today. After going through your problem i again checked my code and i found my mistake.I was using signout() at the end of my 1st activity. After i removed that my code is working fine.

I am using the authStateListener given in the documentation.Set the listener in onCreate method. Funtion getCurrentUser() sets an background task to fetch current user so the listener is important.

onCreate()

mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                String UserId = mCurrentUser.getUid();
                mCurrentUser = user;
                Toast.makeText(ListActivity.this, "USER ID\n"+mUserId,Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(ListActivity.this, "no id got", Toast.LENGTH_SHORT).show();
            }

        }
    };

In onStart or onResume method i am calling the getCurrentUser() method

onResume()

 mCurrentUser = mAuth.getCurrentUser();

in case of slow connection

mCurrentUser = user;

in onCreate() will take care of the problem. Personally i prefer to use getcurrentuser method in onStart() so that it gets some time to fetch the user if the app is running on a slow internet.

I hope this solves your problem. Thank You.

like image 77
Subhajit Das Avatar answered Nov 16 '22 20:11

Subhajit Das