Android stop

I am using a class that extends the Handler class to update the activity UI. The code is as follows:

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 called this handleMessage () method for the RefreshHandler object as follows

 mRedrawHandler = new RefreshHandler(); mRedrawHandler.handleMessage(new Message()); 

But here I ran into one problem that it works even after closing my application.

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

Thanks..

+4
source share
2 answers

Something like this should work:

 if(!isFinishing()) { sendMessageDelayed(obtainMessage(0), delayMillis); } 

i.e. add if-statement around your message line

+1
source

onDestroy, remove call

mRedrawHandler.removeMessages (0);

+8
source

Source: https://habr.com/ru/post/1306668/


All Articles