i want to make users appear online in my app when they are in any activity until they close or minimize the app. below is what i did in MainActivity.java
if (mAuth.getCurrentUser() != null) {
mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}
}
@Override
public void onStart () {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sendtostart();
} else {
mUserRef.child("online").setValue("true");
}
}
@Override
protected void onStop () {
super.onStop();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
mUserRef.child("online").setValue(ServerValue.TIMESTAMP);
}
}
private void sendtostart () {
Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
startActivity(startIntent);
finish();
}
but this only works when user is in the main activity so when a user switched to Chat Activity or Profile Activity they appear offline. below is what i tried in my ChatActivity.java
but it did not work
if (mAuth.getCurrentUser() != null) {
mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}
}
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null){
mUserRef.child("online").setValue("true");
}else {
mUserRef.child("online").setValue("false");
}