Firebase getCurrentUser在新的android活动中返回null [英] Firebase getCurrentUser is returning null in new android activity

查看:166
本文介绍了Firebase getCurrentUser在新的android活动中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LoginActivity:

 私有FirebaseAuth firebaseAuth; 
私有FirebaseAuth.AuthStateListener mAuthListener;

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

firebaseAuth = FirebaseAuth.getInstance();
$ b mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
//用户登录
Log.d(TAG,onAuthStateChanged:signed_in:+ user.getUid());
finish();
startActivity(new Intent(LoginActivity.this,MainActivity.class));

} else {
//用户签出
Log.d(TAG,onAuthStateChanged:signed_out);
}
}
};

$ / code>

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,用户不是NULLL。他的电子邮件是:+ firebaseUser.getEmail());


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

$ / code>

ActivityB:

 私人FirebaseAuth firebaseAuth; 

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_friend);
FirebaseUser user = FirebaseAuth.getInstance()。getCurrentUser();
//这里用户总是为空
}

好的,这正在发生:
我正在成功登录到应用程序,在 MainActivity.java 它打印的用户不是空的。但是当按下按钮时, ActivityB.java 正在启动,但用户和 FirebaseAuth 始终为空。 p>

我之前在 MainActivity.java 中添加了一个 authListener 我发现每当我从MainActivity开始一个新的活动时,监听器被触发,并且它的用户将是空的。

那么为什么当我启动一个Intent时,以及如何从不同的活动访问 firebaseAuth currentUser

解决方案

今天我有同样的问题。经过你的问题后,我又检查了我的代码,发现我的错误。我在第一次活动结束时使用了signout()。我删除后,我的代码工作正常。

我正在使用文档中给出的authStateListener。设置onCreate方法中的侦听器。函数getCurrentUser()设置一个后台任务来获取当前用户,所以监听器是重要的。

onCreate()

<$ (@NonNull FirebaseAuth firebaseAuth){
FirebaseAser user = firebaseAuth.getCurrentUser(){code> );
if(user!= null){
//用户登录
String UserId = mCurrentUser.getUid();
mCurrentUser = user;
Toast.makeText(ListActivity.this,USER ID\\\
+ mUserId,Toast.LENGTH_SHORT).show();

else {
Toast.makeText(ListActivity.this,no id got,Toast.LENGTH_SHORT).show();
}

}
};

在onStart或onResume方法中,我调用getCurrentUser()方法

onResume()

  mCurrentUser = mAuth.getCurrentUser(); 

在连线速度较慢的情况下

  mCurrentUser = user;在onCreate()中的

会解决这个问题。
个人而言,我更喜欢在onStart()中使用getcurrentuser方法,以便在应用程序运行在缓慢的Internet上时获取用户的一些时间。



我希望这能解决你的问题。
谢谢。


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?

解决方案

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.

这篇关于Firebase getCurrentUser在新的android活动中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆