Obtaining the id of the user's session in firebase is not obtained?

Viewed 434

It is working in android studio and managing cell phone authentication in firebase, Can't get user session id in firebase, It works in a first activity but wanting to obtain a second activity is where the problem occurs, and the app closes automatically. The code is in a provider class which is this:

public String getId(){
        if(auth.getCurrentUser() != null){
            return auth.getCurrentUser().getUid();
        }
        else{
            return null;
        }
    }

to get the id of the session in another class of an activity I instantiate the provider class and do this:

Provider provider = new Provider();
Log.d("id",provider.getId());

Someone who can tell me what may be happening, thank you in advance.

1 Answers

Just declare and initialize FirebaseAuth again in the activity where you want to fetch current user's userID.

Try like this:

    public String getId(){

       if( FirebaseAuth.getInstance() != null)
              return  FirebaseAuth.getInstance().getCurrentUser().getUid();
       else
           return null;
       
     }
Related