0

I want to offline the user on the system before the app being closed and delete some files and other things . I tried putting it in ondestroy but the code do not execute. And onstop is called while pausing and this is not what i want. Any ideas guys?

Essam Mahdy
  • 139
  • 1
  • 8
  • Overriding `onBackPressed`? – Yupi Jun 28 '18 at 22:25
  • `onDestroy` is called if you kill the app. `onStop` is called when the app is in background and after `onPause` – Jim Jun 28 '18 at 22:25
  • @jim.. I know.. I ask for solution – Essam Mahdy Jun 28 '18 at 22:51
  • @yupi not back pressed. When the app is closed by the user by pressing x mark for multiple application. I mean forcing it to stop – Essam Mahdy Jun 28 '18 at 22:53
  • "I want to offline the user on the system before the app being closed" I have know idea what this is supposed to mean. Certainly there would be a solution to your problem, but you have not provided enough coherent information to help provide a solution. What is this "...delete some files and other things..." that just absolutely need to be done in the `onDestroy` method that cannot be done in `onStop`? – Barns Jun 28 '18 at 23:46

3 Answers3

0

According to Google's doc, you cannot rely on the onDestroy() method to achieve what you're trying to do.

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. Note: do not count on this method being called as a place for saving data!

Maybe it's better for you to specific in the question what you'd like to do and why it needs to be done before closing the app. Also you could have a look at WorkManager which was just proposed in this year's Google IO. It guarantees your task to be run even if your app is force closed.

rookiedev
  • 1,057
  • 2
  • 9
  • 21
0

One option you have is to create and run a service and use the method onTaskRemoved. See this answer for details - https://stackoverflow.com/a/26882533/5909655

Jayson Chacko
  • 2,388
  • 1
  • 11
  • 16
0

https://developer.android.com/guide/components/activities/activity-lifecycle

After Pausing comes onStop() and onDestroy. It seems that onStop() is the place that you wish to use in your particular case. Check out the documentation above. If that doesn't help post some more details

edit: after reading your question again I came up with a possible solution: when you are manually pausing and do not wish to disconnect what you could do is set a flag, eg "STAY_ONLINE" and in onStop()

If (flag=="STAY_ONLINE") {//do something}
else {//disconnect the user}

(Sorry for formatting, I am on phone :))

N. Matic
  • 213
  • 1
  • 8