Reputation: 197
I have say a user who signs in to my app using one activity via Firebase, can I access the user's details in the next activity. Eg:
Login class:
public class LoginActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
Button button;
public FirebaseAuth Auth = FirebaseAuth.getInstance();
public FirebaseAuth.AuthStateListener authlistener;
String email, pass;
ProgressBar bar;
public View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick (View view){
email = editText.getText().toString().trim();
pass = editText2.getText().toString().trim();
logInUser();
}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.button2);
Auth = FirebaseAuth.getInstance();
button.setOnClickListener(buttonListener);
bar = (ProgressBar)findViewById(R.id.progressBar2);
bar.setVisibility(View.INVISIBLE);
authlistener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
} else {
// User is signed out
}
// ...
}
};
// ...
}
@Override
public void onStart() {
super.onStart();
Auth.addAuthStateListener(authlistener);
}
@Override
public void onStop() {
super.onStop();
if (authlistener != null) {
Auth.removeAuthStateListener(authlistener);
}
}
public void logInUser(){
bar.setVisibility(View.VISIBLE);
Auth.signInWithEmailAndPassword(email, pass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Intent loggedInActivity = new Intent(getApplicationContext(), Loggedin.class);
startActivity(loggedInActivity);
}
else{
Toast.makeText(getApplicationContext(), "There was an error, try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Logged in Activity:
public class Loggedin extends AppCompatActivity {
Button changeMail, changePass;
String oldPassword;
String newPassword;
EditText oldpass, newpass;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
public FirebaseAuth Auth = FirebaseAuth.getInstance();
public void updatePassword(){
oldPassword = oldpass.getText().toString();
newPassword = newpass.getText().toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loggedin);
changeMail = (Button)findViewById(R.id.changeMail);
changePass = (Button)findViewById(R.id.changePass);
oldpass = (EditText)findViewById(R.id.oldPass);
newpass = (EditText)findViewById(R.id.newPass);
oldpass.setVisibility(View.INVISIBLE);
newpass.setVisibility(View.VISIBLE);
}
}
Say I want to change the password of the user in the loggedinClass
. Do I have to uses the intent from the "login.class
" and then use the sign in
method in the loggedin
class rather than the login.class.
but what if I had multiple activities, this would get tedious, signing in to every activity?
Upvotes: 1
Views: 3610
Reputation: 124
You don't need a listener for Firebase... its a singleton and available throughout your activities. Just add this to your base activity
public FirebaseUser getFirebaseUser() {
return FirebaseAuth.getInstance().getCurrentUser();
}
or use if(FirebaseAuth.getInstance().getCurrentUser() != null) { ...} where you need it
Upvotes: 3
Reputation: 600006
If you put an AuthStateListener
in the other classes, you can detect the sign-in state of the user without requiring them to sign in again.
authlistener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
} else {
// User is signed out
}
// ...
}
};
FirebaseAuth.getInstance().addAuthStateListener(authListener);
Upvotes: 2