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?
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 adapt your app design to fit this lifecycle.
You shouldn't try to find a workaround, it can just make things worse : what if the user runs out of battery, or if your app crashes? all the data will be lost. I don't know what kind of data you're working with, but this usually result in a poor rating on Google Play. Even if something bad happens, all the data should already been saved (AsyncTasks
are great for this, don't be afraid to start one on every change to constantly save data).
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.
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
}
}
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);
}
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