简体   繁体   中英

On destroy does not get call when app closes from recents - Android

I am trying to create a mock location app. After the user chose a location, the app should spoof his location to the place he chose.

I want that when the user closes the app, from recent apps or by pressing the back button the app will stop to mock location.

After reading the docs and some debugging, I understand that the OnDestroy function does not get called when the user stop the app from recent apps.

How can I know when the user decided to kill the app?

Yes, there is no guarantee that onDestroy method will be called everytime.onDestroy is called when Android goes short of resources and it need to clean up to recover some memory resources.

So, the best solution is to properly use onStop callback for deleting the location reference and initilize again in onStart.

For more details, check following links: https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

Activity OnDestroy never called?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM