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

python - Alternative to referencing variable before assignment -

vb.net - How to ignore if a cell is empty nothing -

Sort a complex associative array in PHP -