This paper analyzes the OnDestroy () invocation method in the activity of Android programming. Share to everyone for your reference, specific as follows:
Just a bug let me find that if the activity implements a callback interface and then uses this to set the method that requires the callback interface, this scenario is more common and the most common is to implement the Onclicklistener interface and then Findviewbyid (). Setonclicklistenr (This)
If the callback interface is set to a static object (a single case pattern), the activity will not be invoked OnDestroy () when the activity is finish (by pressing the return key to the desktop), possibly because the activity object is still being referenced!
At this point you click the icon back to the application, OnCreate () Call again!
Obviously, if you put the release of resources in the OnDestroy (), it will lead to memory leaks !
Is there any way to solve it? Some
You can Judge Isfinishing () in the OnPause () method, after normal call finish () the activity of the callback process is OnPause, onStop, OnDestroy, if the above situation, only to onpause! But the isfinishing () flag is still true! You can release the resources.
Let's take a look at OnDestroy's official explanation:
protected void OnDestroy () Added in API Level 1 perform the "Any final cleanup before" is destroyed. This can happen either because the "activity is finishing" someone called Finish () on it, or because the system is Temporar Ily destroying this instance's activity to save space.
You can distinguish between this two scenarios with the Isfinishing () method. Note:do not count on this method being called as a place for saving data! For example, if it's editing data in a content provider, those edits should to committed in either OnPause () or Onsaveinstancestate (Bundle), not here. This are usually implemented to free resources like threads that are associated with the activity of so that a Destroye D activity does not leave such things around while the rest of the IT application is still. There are situations where the system would simply kill the activity ' s hosting process without calling Others) in it, so it should is used to do thinGS that are intended to remain around after the process goes away. Derived classes must call through to the Super class ' s implementation of this method.
If They does not, an exception would be thrown.
I hope this article will help you with your Android programming.