LoginActivity:
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:
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:
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.
发生的情况是,我成功登录到应用,在MainActivity。java它打印的用户不是null。但是当我按下按钮时,ActivityB。java正在启动,但是用户和FirebaseAuth始终为空。
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.
我之前在MainActivity中添加了一个authListener。java和我发现,每当我从主活动启动一个新活动时,侦听器就会被触发,它的用户将是空的。
So why is the listener triggered when I start an Intent, and how do I access the firebaseAuth
and currentUser
from different activities?
那么,当我开始一个意图时,为什么会触发侦听器,我如何从不同的活动访问firebaseAuth和currentUser ?
1 个解决方案
#1
1
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.
我今天遇到了同样的问题。在检查了你的问题之后,我再次检查了我的代码,发现了我的错误。我在第一次活动结束时使用了signout()。在我删除代码之后,我的代码运行良好。
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.
我正在使用文档中给出的authStateListener。在onCreate方法中设置监听器。函数getCurrentUser()设置一个后台任务来获取当前用户,因此监听器很重要。
onCreate()
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
在onStart或onResume方法中,我调用getCurrentUser()方法。
onResume()
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.
onCreate()将处理这个问题。就个人而言,我更喜欢在onStart()中使用getcurrentuser方法,这样当应用程序运行在一个缓慢的internet上时,就可以获得一些时间来获取用户。
I hope this solves your problem. Thank You.
我希望这能解决你的问题。谢谢你!
#1
1
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.
我今天遇到了同样的问题。在检查了你的问题之后,我再次检查了我的代码,发现了我的错误。我在第一次活动结束时使用了signout()。在我删除代码之后,我的代码运行良好。
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.
我正在使用文档中给出的authStateListener。在onCreate方法中设置监听器。函数getCurrentUser()设置一个后台任务来获取当前用户,因此监听器很重要。
onCreate()
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
在onStart或onResume方法中,我调用getCurrentUser()方法。
onResume()
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.
onCreate()将处理这个问题。就个人而言,我更喜欢在onStart()中使用getcurrentuser方法,这样当应用程序运行在一个缓慢的internet上时,就可以获得一些时间来获取用户。
I hope this solves your problem. Thank You.
我希望这能解决你的问题。谢谢你!