Android chat app Checking online/offline users firebase

I am trying to add online/offline feature in my android chat app using .info/connected path I wrote the following code inside onCreate() method

studentref = FirebaseDatabase.getInstance().getReference("student").child(user.getUid());
    connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");

connectedRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            boolean connected = snapshot.getValue(Boolean.class);
            if (connected) {
                studentref.child("status").onDisconnect().setValue("offline");
                studentref.child("status").setValue("Online");
            } else {
                studentref.child("status").setValue("offline");
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });

But the else part does not execute when i minimized the app for more than 60 seconds It only works when i killed the app or when i switch off the internet for more than 60 seconds

How to make it works , when the app is in foreground it should set the value “online” and when the app is in background or killed it should set the value to “offline”

Answer

I solved this issue using 2 ways First we need to detect when the app goes to the background and come back to the foreground , so when the app goes to foreground update the user state as “Online” when the app goes to background update the user state as “Offline”

We can achieve this by implementing LifecycleObserver class and writing the following 2 methods

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppBackgrounded() {
        //App in background
        // here update user state as offline 
        
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppForegrounded() {
        // App in foreground
        // here update user state as online
          
    }

But this will not work when there is no internet connection , Eg: when the user switch off the internet while the app in foreground and close the app in this case the user state will remain online even the app is closed , To solve this we need also to check the connection to .info/connected path

connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
        listenerCon = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                boolean connected = snapshot.getValue(Boolean.class);
                if (connected) {
                    user= FirebaseDatabase.getInstance().getReference("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                    user.child("state").setValue("Online");
                    user.child("state").onDisconnect().setValue(ServerValue.TIMESTAMP);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        };
        connectedRef.addValueEventListener(listenerCon);

So when there is no connection for more than 60 sc the user state will be updated to offline