Android: onDestroy() or similar method in Application class

落爺英雄遲暮 提交于 2019-12-28 04:06:40

问题


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 other onCreate() in activities, but I would like to know if there is onDestroy() or similar method in the Application class that could be overridden so that I would be able to store variables in persistent memory, unregister listener and send last message to server before app process gets killed? If not, is there any other way to do that?


回答1:


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.




回答2:


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.




回答3:


First of all: I'm an absolute beginner

I need to execute some code when my app exits (yes, I know not such thing in Android) and this works ok for me:

-I have MyApplication wich extends Application. As a member of MyApplication there is an AtomicInteger field named activeActivitiesNumber and a public getter method.

-All the application activities extend MyActivy (which itself extendes Activity)

-MyActivity overrides onCreate, onResume and onStop methods and also have a protected field: Protected MyAppication mAppState;

a) OnCreate(){
super.onCreate();
mAppState=this.getApplication();...}


b) onResume(){
super.OnResume();
myAppState.getactiveActivitiesNumber().addAndGet(1)
....}

c) onStop(){
super.onStop()

if (myAppStatemyAppState.getactiveActivitiesNumber()..decrementAndGet()<1){
...call exiting code (for instance a public method defined in MyApplication}
}

It has a problem: if you start any activity tan doesn´t belong to your application (for instance send an email) it will fire the exiting method.

Another problem (don´t know if it is a real o theoretical one) is that there is no guarantee tan in some situations onStop will be called.

Hope this help.




回答4:


You can override onDestroy() in the Activity which will be the last one closed in your app and check if it's finishing. In this case your code will not be invoked on a device rotation. But you should be aware that onDestroy() is not invoked when an app is closed through device home button.

@Override
public void onDestroy(){
    super.onDestroy();
    if(isFinishing()){
        //do your stuff here
    }
}



回答5:


You can use registerActivityLifecycleCallbacks() in the Application class with the following callbacks (I recommend creating an AppLifecycleCallbacks class that extends the ActivityLifecycleCallbacks):

public interface ActivityLifecycleCallbacks {
    void onActivityCreated(Activity activity, Bundle savedInstanceState);
    void onActivityStarted(Activity activity);
    void onActivityResumed(Activity activity);
    void onActivityPaused(Activity activity);
    void onActivityStopped(Activity activity);
    void onActivitySaveInstanceState(Activity activity, Bundle outState);
    void onActivityDestroyed(Activity activity);
}



回答6:


Another option you have is to override onDestroy() on the first screen of your app, so it will be called right before your process gets killed!



来源:https://stackoverflow.com/questions/17278201/android-ondestroy-or-similar-method-in-application-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!