Android passes the firebase auth object to another activity to exit
•
Android
I log in to the user in the main activity, but I want to log out of the user in another activity through the button. However, I can't pass the firebaseauth object to other activities
Putparcel and putserializable don't work because I can't control the class itself. Sharing preferences only accept the original type. Should I get a new instance in the new activity and log off the user in the new activity? Even if I get a new instance, will the user's state be preserved?
This is the code
mAuth = 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());
Toast.makeText(getApplicationContext(), "You are logged in!!!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, CameraActivity.class);
startActivity(intent);
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
resolvent:
Firebase auth is a singleton class. You can get an instance of firebase auth from anywhere in the application
You just need to add
mAuth=FirebaseAuth.getInstance();
// Firebase sign out
mAuth.signOut();
Or a simple way
.FirebaseAuth.getInstance()signOut();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码