Handler in Android use should be aware of the problem (solve the oom memory leak caused by handler)

Source: Internet
Author: User

More recently, Handler has been used frequently during the project to handle actions on some UI threads, as well as the use of handler postdealy. However, after the use of the handler is not processed, resulting in memory overflow phenomenon, through Google, found a solution to this problem. The relevant code in the project is posted below

 /********************************** the following code resolves a memory leak that handler might cause ***************************************************/    /*** Create a static inner class*/    Private classMyHandlerextendshandler{//when you hold a weak reference, HANDLERACTIVITY,GC is recycled.        Private FinalWeakreference<replydetailsactivity>mactivty;  PublicMyHandler (replydetailsactivity activity) {mactivty=NewWeakreference<replydetailsactivity>(activity); } @Override Public voidhandlemessage (Message msg) {replydetailsactivity activity=Mactivty.get (); Super. Handlemessage (msg); if(activity!=NULL){            }        }    }    //execute the appropriate business logic    Private FinalRunnable myrunnable =NewRunnable () {@Override Public voidrun () {//Execute our business logic            if(sumpages>1){                 for(inti = 2; I <=sumpages; i++) {getreplydetails (string.valueof (i)); }            }        }    };

Call Inside the OnCreate

MyHandler Mhandler = new MyHandler (this);        Mhandler.postdelayed (myrunnable,2000);

The source of this solution is in the blog of the Great God: http://blog.csdn.net/javazejian/article/details/50839443

Handler in Android use should be aware of the problem (solve the oom memory leak caused by handler)

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.