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)