Android: onDestroy() or similar method in Application class

前端 未结 5 1519
清酒与你
清酒与你 2020-12-03 09:22

I am extending Application class to work with some global variables that need context. I know there is onCreate() method in the Application class that gets called before any

5条回答
  •  鱼传尺愫
    2020-12-03 10:14

    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.

提交回复
热议问题