How to stop the handler?
Asked Answered
M

3

19

I have a doubt .. Am having a hander in an activity in my application. Though my activity destroyed the handler still functioning. Is it running on different process other than the application process ? Could any one plz explain why its working like so ? Is it possible to stop the handler while onDestroy of the activity ?

Thanks in advance.

Mathilda answered 18/2, 2011 at 5:42 Comment(1)
It would be helpful if you gave details of the handler, even better if you posted the code. Also, do you instantiate the handler on a different thread?Palaeozoology
I
26

As described in the documentation

http://developer.android.com/reference/android/os/Handler.html

"Each Handler instance is associated with a single thread and that thread's message queue."

When you are about to finish your activity, e.g. in onDestroy() you also need to cancel the callback for the runnable it was started for:

mHandler.removeCallbacks(previouslyStartedRunnable);

You can do that even without checking if runnable was already fired while your activity was active.

UPDATE:

There are two additional cases to be considered:

1.) You have implemented your Handler in a way that you created new class for the Runnable, e.g.

private class HandleUpdateInd implements Runnable...

Usually you need to do that if you have to start delayed runnable with current set of parameters (which may change until runnable fires). To cancel it you need to use

mHandler.removeCallbacksAndMessages(HandleUpdateInd.class);

2.) If you are using inline call (JPM thanks for the comment)

handler = new Handler() { public void handleMessage(Message msg) { ... } };

Then you need to define "what" value for that Message. Later on, if you need to cancel it you can use

handler.removeMessages(what); 

to perform that task.

Integumentary answered 18/2, 2011 at 7:4 Comment(2)
how do you do this if its an inline call in your onCreate of an Activity? i.e. handler = new Handler() { public void handleMessage(Message msg) { ... } };Part
When sending the Message, you have to define "what" value. Then you can later on refer to it and request handler.removeMessages(what);Integumentary
P
1

do you mean you have a thread somewhere in your activity? stopping a thread is quite easy, you need to have a reference for that thread. like:

private Thread mMyThread;
mMyThread = new Thread .......

and when you want to stop it, just check if the reference isn't null and call interrupt method:

if (mMyThread != null) {
    mMyThread.interrupt();
}

hope it will help

Putrefaction answered 18/2, 2011 at 6:59 Comment(0)
C
0

I like Zelimir's answer, just some more info on the matter - Android doesn't define limits for an application life, the application is created on first start, and may remains alive between several Activities onCreate/onDestroy cycles. Handlers are associated with Application single main event thread/queue, so an activity can't "stop" it (though you probably could do that via Handler.getLooper().quit(), but I'm not sure if it won't cause problems. Also you could kill whole application as described here here).

Probably Android concept is to reuse Applications instances as much as possible, so your primary concern when developing an Activity is proper clearance of Activity's resources on onDestroy call. Static variables, threads, AsyncTasks and messages/callbacks in handlers are the items to worry about.

Charteris answered 16/12, 2013 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.