0

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?

Eduard
  • 111
  • 1
  • 10
  • 2
    This is a complex question, there are a lot of responses, [try this one](https://stackoverflow.com/a/42679191/15298643) if that doesn't work, the whole post can help you understating lifecycles – javdromero Apr 05 '21 at 00:31
  • After looking at Android Developers activity life cycle page, I have made some changed which are in the Edit. – Eduard Apr 05 '21 at 00:52
  • This will leave the primary thrust of your question yet to solve, the answer to the edit 2 question is that you can't count on onDestroy being called reliably (reference: https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29). Sometimes your process is just killed. The solution is likely going to involve some simple service that runs alongside your app and makes the desired update in the desired conditions. Not sure how you'd do this - I'm interested to see the solution you find. You used to be able to use onTaskRemoved(), but as of Android O no dice there. – RyeBread Apr 05 '21 at 01:03
  • Just had an idea - see answer below. – RyeBread Apr 05 '21 at 01:23

2 Answers2

0

For my particular case, I have an activity that never finishes unless the app is permanently closed, and the app is no longer running, not even in the foreground, or the user manually logs out.

Because of this I can call onDestroy() and add the code i want to run in there. NOTE, if you are calling on super.onDestroy(), you need to put it at the end of the code you want to run so that the acitivity does not end prior to running the code. Exmaple below:

@Override
    protected void onDestroy() {
        /*
       RUN YOUR CODE HERE
        */
        super.onDestroy();
    }
Eduard
  • 111
  • 1
  • 10
0

I believe you will be able to do this using WorkManager (reference: https://developer.android.com/topic/libraries/architecture/workmanager/basics). If you create a worker to do some work after a delay using setInitialDelay, then in the doWork method check if your app is running using something like this: How can I check if an app running on Android?. If your app is running, enqueue the same work again after the same delay - basically schedule the same check to happen after a few minutes or something. If your app is not running, the app's been closed - run your code.

RyeBread
  • 170
  • 1
  • 9