I am using Firebase for my database and also I am using FirebaseAuth to login and logout users in my app. When a user logs into the application, I want to update a value called status (login status for clarity) to true. Similarly, I want this value to be set to false when the user logs out of the application. However, a user might close the application without logging out and I want to update the status value when the user closes the application as if they logged out.
Firebase logs out the user automatically when they close the application, as they have to log back in if they open it again.
EDIT
For reference below is my code that i want to run when the application is closed.
FirebaseDatabase.getInstance().getReference("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("status").setValue(false);
I have tried running this code on onDestroy() Ovewritten method for all of my activities. However, this is not the solution because if the user navigates from one activity to another, the code will trigger because i use finish() when i close certain activities. This isn't ideal and I only want the code to trigger only when the application is closed.
EDIT 2
Instead of calling onDestroy() on all of the activities, i now only call onDestroy() with the code that i want to run on one activity only. It is important to note that htis activity never finishes, unless the user closes the application or manually logs out.
However when the application is swiped away, this code does not trigger, why is that?