I'm trying to make it so when a user loses the connection to the database they get sent to the previous activity. However, what ends up happening is that on the activities creation, this database reference returns false and immediately boots the user back to the previous screen in which case they have to try again. Why is Firebase returning false as soon as this reference is obtained and what can I do to prevent that?
The obvious solution is to add a sort of switch once the user is connected that then allows the disconnected section to trigger. However, I would like to know what causes false to be returned immediately and how to prevent it from doing so. Thank you.
DatabaseReference referenceConnection = database.getReference(".info/connected");
referenceConnection.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
boolean isConnected = snapshot.getValue(Boolean.class);
if(isConnected){
Log.e("CONNECTION", "REGAINED");
searchForRooms();
}
else{
// This returns immediately despite being connected
Log.e("CONNECTION", "LOST");
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});