Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stopping android handler loop

Tags:

android

i am using a class which extends Handler class to update my activity UI. The code is as follows in side main activity,

public class RefreshHandler extends Handler {

     public void handleMessage(Message msg) {


      Homeform.this.updateUI();
     }
     public void sleep(long delayMillis) {
      this.removeMessages(0);
         sendMessageDelayed(obtainMessage(0), delayMillis);
     }
 };

 private void updateUI(){
  Log.v(""," I am inside Update UUI=====================");

  refresh();
     mRedrawHandler.sleep(5000);

 }

And i had call this method handleMessage() on the object of RefreshHandler as follows

mRedrawHandler = new RefreshHandler();

mRedrawHandler.handleMessage(new Message());

But here i am facing one problem that it is running after closing my application also .

please solve my problem to stop this handler when close this application.

Thank you..

like image 629
VenkaReddy Avatar asked Jan 22 '23 06:01

VenkaReddy


1 Answers

onDestroy, call remove

mRedrawHandler.removeMessages(0);

like image 109
Pedro Dacunos Jr Avatar answered Feb 02 '23 15:02

Pedro Dacunos Jr