Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Quitting the Looper?

I have a thread I use to periodically update the data in my Activity. I create the thread and start a looper for using a handler with postDelay(). In onDestroy() for my activity, I call removeCallbacks() on my handler.

Should I then call handler.getLooper().quit()? Or not worry about it and let the OS deal with it? Or would it just run forever then, consuming CPU cycles?

like image 435
stormin986 Avatar asked May 08 '10 06:05

stormin986


People also ask

How do you end a looper?

To kill it you must quit the loop. To do that, you need to run the quit command on the context of that thread. And don't forget to null all reference to views and activities.

How do you terminate Looper without processing any more messages in the message queue?

Be sure to call quit() to end the loop. Quits the looper. Causes the loop() method to terminate without processing any more messages in the message queue.

Why do we use Looper in Android?

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare in the thread that is to run the loop, and then loop to have it process messages until the loop is stopped.

What is handler and looper in Android?

Looper is an abstraction over event loop (infinite loop which drains queue with events) and Handler is an abstraction to put/remove events into/from queue with events (which is drained by Looper) and handle these events when they are processed.


1 Answers

According to the Android Documentation you should call quit().

When you call Looper.loop() a while loop is started. Calling Looper.quit() causes the loop to terminate. The garbage collector cannot collect your object while the loop is executing.

Here are the relevant section from Looper.java:

public static final void loop() {
    Looper me = myLooper();
    MessageQueue queue = me.mQueue;
    while (true) {
        Message msg = queue.next(); // might block
        //if (!me.mRun) {
        //    break;
        //}
        if (msg != null) {
            if (msg.target == null) {
                // No target is a magic identifier for the quit message.
                return;
            }
            if (me.mLogging!= null) me.mLogging.println(
                    ">>>>> Dispatching to " + msg.target + " "
                    + msg.callback + ": " + msg.what
                    );
            msg.target.dispatchMessage(msg);
            if (me.mLogging!= null) me.mLogging.println(
                    "<<<<< Finished to    " + msg.target + " "
                    + msg.callback);
            msg.recycle();
        }
    }
}

public void quit() {
    Message msg = Message.obtain();
    // NOTE: By enqueueing directly into the message queue, the
    // message is left with a null target.  This is how we know it is
    // a quit message.
    mQueue.enqueueMessage(msg, 0);
}
like image 119
Jonathan Avatar answered Sep 25 '22 02:09

Jonathan