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

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

问题描述

登录活动:

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

主要活动:

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

活动B:

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
  }

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

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.

我之前在 MainActivity.java 中添加了一个 authListener,我发现每当我从 MainActivity 启动一个新活动时,监听器都会被触发,它的用户将为空.

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.

那么为什么在启动 Intent 时会触发侦听器,以及如何从不同的 Activity 访问 firebaseAuthcurrentUser?

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

推荐答案

我今天遇到了同样的问题.在解决了你的问题后,我再次检查了我的代码,我发现了我的错误.我在第一个活动结束时使用了 signout().在我删除后,我的代码工作正常.

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.

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

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
"+mUserId,Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(ListActivity.this, "no id got", Toast.LENGTH_SHORT).show();
            }

        }
    };

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

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

onResume()

 mCurrentUser = mAuth.getCurrentUser();

在连接速度慢的情况下

mCurrentUser = user;

在 onCreate() 中会解决这个问题.就我个人而言,我更喜欢在 onStart() 中使用 getcurrentuser 方法,以便在应用程序在慢速互联网上运行时有时间获取用户.

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天全站免登陆