问题
I have 2 activities in my app. I also have a sign-in button that when clicked this method is called:
private void signIn() {
firebaseAuth.signInAnonymously().addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
goToSecondActivity();
}
}
});
}
This my goToSecondActivity()
method:
private void goToSecondActivity() {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
finish();
}
As specified in the official doc, in onStart()
, I check the firebaseUser
object for nullity like this:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
goToMapActivity();
}
In the second activity I'm trying to get the uid of the user like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
If I try to print this uid, everything works fine, the uid is printed. If I try to sign-out (in the SecondActivity) and sign-in again, a new uid is generated, which is correct. The problem is if I sign-out again and uninstall the app and launch it again, an old user is used, even if I wait for 5 hours.
How can I solve this, so if I sign-out, next time I use my app to use the last generated user and not a very old one?
回答1:
It's because Android 6 has the automatic backup. You need to tune android:allowBackup
and android:fullBackupContent
in your manifest application>
tag if you don't want your data backed up or if you want to include or exclude some resources. It's not a bug.
You can find more info here
In short to avoid this behaviour it is necessary to put android:allowBackup="false"
and android:fullBackupContent="false"
in manifest.xml
来源:https://stackoverflow.com/questions/50622674/firebaseuser-is-not-null-after-anonymous-authentication-is-signed-out