OnDestroy () invocation analysis in the activity of Android programming _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.