What led to a context leak:handler& inner class

Source: Internet
Author: User

Reference: http://www.cnblogs.com/kissazi2/p/4121852.html

Why is the internal custom handler class to be static type?

Reason:

1. When an Android application starts for the first time, the Android framework creates a Looper object for the application's main thread. A looper implements a simple message queue that processes the message object in a loop. All major application framework events (such as activity life cycle method calls, click Buttons, and so on) are contained in the Message object, which is then added to the Looper messages queue and then processed. The looper of the main thread exists throughout the lifetime of the application.

2. When a handle is instantiated in the main thread, it is associated to the Looper message queue. Messages sent to the message queue hold a handler reference so that the Android framework can invoke Handler#handlemessage (message) when Looper finally processes the message.
3. In Java, non-static inner classes and anonymous classes implicitly hold a reference to their external class. Static inner classes are not

Explanation: The message in handler holds the handler reference, and handler is an anonymous inner class object that holds activity. If MSG is a delay operation then when actvity finish, the Act reference is held, causing The objects in the activity are not freed to cause a memory leak.

The correct code:

Both the 1.handler and Runable methods declare static, which is the inner class that is used in handler (the anonymous class cannot be used) and is declared as a static type

Use weak references in 2.handler to hold activity or context objects

 Public classSampleactivityextendsActivity {/*** Static instances of anonymous classes do not implicitly hold references to their external classes*/    Private Static FinalRunnable srunnable =NewRunnable () {@Override Public voidrun () {}}; Private FinalMyHandler Mhandler =NewMyHandler ( This); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //delay 10 minutes to send a message.Mhandler.postdelayed (srunnable, 60 * 10 * 1000); //return to previous activityfinish (); }    /*** Instances of static inner classes do not implicitly hold references to their external classes. */    Private Static classMyHandlerextendsHandler {Private FinalWeakreference<sampleactivity>mactivity;  PublicMyHandler (sampleactivity activity) {mactivity=NewWeakreference<sampleactivity>(activity); } @Override Public voidhandlemessage (Message msg) {sampleactivity activity=Mactivity.get (); if(Activity! =NULL) {                // ...            }        }    }}

What led to a context leak:handler& inner class

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.