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

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -