Android: onDestroy() or similar method in Application class
There is no such call back on a production device for the Application class.
The things you want to do should usually be done right after the changes are made, or in the onPause()
of the respective app component.
In android, there is no concept of closing an app. The user just leaves: this is the only event that you will be aware of (onPause()
in an activity). You should design your app so that it fits this lifecycle.
Typically, you should save any changes immediately but asynchronously, so that the UI doesn't hang. This is much better than saving changes in onPause()
because if something bad happens before the app is paused (the app crashes, the user runs out of battery), all data was already saved properly.
SharedPreferences
already save changes asynchronously so if you use that, you have nothing else to do. Otherwise you can use Kotlin coroutines or if you use Java, the good old AsyncTask
is great.