android - View's handler not being cleared when view is destroyed -


i noticed view's handler not being cleared when view destroyed. looking @ sample code:

handler viewhandler = view.gethandler(); if (viewhandler != null) {     viewhandler.post(new runnable() {         @override         public void run() {             view.dosmth();         }     }); } 

on rare occassion might throw nullpointerexception inside run() method.

do guys have solution problem, is not creating separate handler in every class (and managing manually) whenever want perform action on ui thread?

try code

handler viewhandler = view.gethandler();     if (viewhandler != null) {         runnable runnable=new runnable() {             @override             public void run() {                 view.dosmth();             }         };         viewhandler.post(runnable);     } 

and in onstop()

@override protected void onstop() {     super.onstop();     viewhandler.removecallbacks(runnable);   } 

Comments

Popular posts from this blog

unix - grep command not match start of new line with another regex -

sql - calculate weighted average for each day and id based on time intervals in PostgreSQL -

Sort a complex associative array in PHP -