Firebase Read and Write Database Guide lines:
https://firebase.google.com/docs/database/android/read-and-write
public class NavigationActivity extends AppCompatActivity{
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeAuthListener();
mDatabase = FirebaseDatabase.getInstance().getReference();
}
private void initializeAuthListener() {
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
firebaseUser = firebaseAuth.getInstance().getCurrentUser();
try {
if (firebaseAuth != null) {
loadUserDetails();
Log.d("@@@@", "thread:signed_in:" + firebaseUser.getUid());
} else {
Log.d("@@@@", "thread:signed_out");
Intent login = new Intent(NavigationActivity.this, LoginActivity.class);
startActivity(login);
finish();
}
}catch (Exception e){
Intent login = new Intent(NavigationActivity.this, LoginActivity.class);
startActivity(login);
finish();
}
}
};
mAuth.addAuthStateListener(mAuthListener);
}
private void loadUserDetails() {
DatabaseReference userReference = mDatabase
.child("users").child(firebaseUser.getUid());
// displayUserDetails(userReference);
userReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
tv_user_name.setText(user.getDisplayName());
tv_user_email_nav.setText(user.getEmail());
Glide.with(NavigationActivity.this)
.load(user.getPhotoUrl())
.placeholder(R.mipmap.profile)
.centerCrop()
.dontAnimate()
.bitmapTransform(new CropCircleTransformation(NavigationActivity.this))
.into(iv_user_image);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Toast.makeText(ThreadActivity.this, R.string.error_loading_user, Toast.LENGTH_SHORT).show();
// finish();
}
});
}
}
https://stackoverflow.com/a/45328201/5973946