Android

Explained – Android Looper and handler

In this video we discuss about the concept of message queue and how looper and handler are used with the message queue...



In this video we discuss about the concept of message queue and how looper and handler are used with the message queue to update the UI from another thread. To get the background as to why we need to use looper and handler you can watch another video that explains the concept of looper and handler.

Follow me on Twitter: https://twitter.com/anilvdeshpande

Link to previous video: https://www.youtube.com/watch?v=kpFwxJFYnOo

source

46 Replies to “Explained – Android Looper and handler”

  1. Hi sir, I would like to correct you on this. The handler actually automatically has the reference to the message queue of the thread that is creating it. So here you are creating the handler on the UI thread, so it has the reference of UI thread's message queue. Hence you do not need to pass the main looper to it. Handler handler = new Handler(getApplicationContext().getMainLooper()) can be just written as Handler handler = new Handler();

  2. 1:44 demo of crashing app when updating main thread from separate thread
    2:35 Message Queue is Queue of Tasks
    3:19 Looper
    3:54 Handler
    4:28 Demo of Looper and Handler
    5:44 another simpler way of updating UI thread from separate thread
    5:55 textViewThreadCount.post(new Runnable() {
    @override
    public void run () {
    textViewThreadCount.setText(..)
    }
    });

  3. @Codetutor I spent so much time on these topics to understand, but now I came to a conclusion that I should have watched your videos before. It saved a lot of time.

    One thing about code, you don't have to instantiate your Handler using getMainLooper() method. You could have just used Handler() with no arguments as long as you are instantiating it in the UI thread. getMainLooper() is only needed if you instantiate Handler from a non UI thread.

Beri komentar...