android - Cancel the runnable/handler in a custom view when the activity pauses -


i have custom view has blinking cursor. make blinking cursor using handler , posting runnable after 500 milisecond delay.

when activity view in, want stop blinking removing callbacks on handler. however, i've noticed when switch app, handler/runnable keep going, ie, log says still blinking.

if had control of view this

@override protected void onpause() {      handler.removecallbacks(runnable);      super.onpause(); } 

but custom view part of library , don't have control on activities other developers use custom view in.

i tried onfocuschanged, onscreenstatechanged, , ondetachedfromwindow none of these work when user switches app.

here code. simplified removing not pertinent problem.

public class mycustomview extends view {      static final int blink = 500;     private handler mblinkhandler;      private void init() {         // ...         mblinkhandler = new handler();          mtextstorage.setonchangelistener(new mongoltextstorage.onchangelistener() {             @override             public void ontextchanged(/*...*/) {                 // ...                 startblinking();             }         });     }      runnable mblink = new runnable() {         @override         public void run() {             mblinkhandler.removecallbacks(mblink);             if (shouldblink()) {                 // ...                 log.i("tag", "still blinking...");                 mblinkhandler.postdelayed(mblink, blink);             }         }     };      private boolean shouldblink() {         if (!mcursorvisible || !isfocused()) return false;         final int start = getselectionstart();         if (start < 0) return false;         final int end = getselectionend();         if (end < 0) return false;         return start == end;     }      void startblinking() {         mblink.run();     }      void stopblinking() {         mblinkhandler.removecallbacks(mblink);     }      @override     protected void onfocuschanged(boolean focused, int direction, rect previouslyfocusedrect) {         if (focused) {             startblinking();         } else {             stopblinking();         }         super.onfocuschanged(focused, direction, previouslyfocusedrect);     }      @override     public void onscreenstatechanged(int screenstate) {         switch (screenstate) {             case view.screen_state_on:                 startblinking();                 break;             case view.screen_state_off:                 stopblinking();                 break;         }     }      public void onattachedtowindow() {         super.onattachedtowindow();         startblinking();     }      @override     public void ondetachedfromwindow() {         super.ondetachedfromwindow();         stopblinking();     } } 

i guess starting thread separately using thread.run(), instead make method , call recursively this:

public void blink(){      mblinkhandler.postdelayed(mblink, blink); }  

and in runnable:

runnable mblink = new runnable() {     @override     public void run() {         mblinkhandler.removecallbacks(mblink);         if (shouldblink()) {             // ...             log.i("tag", "still blinking...");            blink();         }     } }; 

as directly starting thread using run method. won't stop removing callbacks.

hope helps.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -