Understanding that Looper in Android

I had to add Looper to the following code:

public class MyRunnable implements Runnable { @Override public void run() { Looper.prepare(); final Looper looper = Looper.myLooper(); new Handler().postDelayed( new Runnable() { @Override public void run() { try { } catch (Exception ex) { } finally { looper.quit(); } } }, 100); Looper.loop(); } } 

Please note that I have runnable inside runnable. A nested runnable is executed through a handler. I initially did not have Looper, but Android complained that I needed to call Looper.prepare before executing another thread.

I read Looper, but it still seems cryptic. It seems to act like some kind of internal message pipeline. I do not understand why this is necessary, since there are no messages going from my external runnable to my internal runnable. Although true, it seems that Android is just making a tough rule: if you call a thread from a thread, you MUST also call Looper.prepare. Even if I take it as it is, it still doesn't help to understand why I need to call looper.loop and looper.quit. If I omit Looper.loop, my handler never starts, and this is not clear. What does Looper.loop do to make my handler work?

+6
source share
3 answers

Here is a great article about it.

Looper and Handler on Android

It comes with a simple circuit that leads to a direct understanding of the relationship between Loopers and Handler .

In this diagram, we see that inside one thread (represented by a large rectangle), regardless of how many created handlers they will all have to use the same Looper , that is, a unique looper of this thread.

Handlers & loopers

Note

Looper must be prepared to allow the associated handler to handle published messages .

The Android application, more precisely, the Android UI thread (main thread), already comes with a ready looper ( mainLooper ).

Here's how to chat with the user interface thread .

+8
source

Simple looper concept:

  • Each workflow that you create and run ends after its last operation.

  • To prevent the thread from completing, you can start the loop by calling Looper.loop() , think of it as while(true){} . Before calling Looper.loop() you need to prepare the loop with Looper.prepare() , if not already prepared. To end the loop and end the thread, you need to call looper.quit() in the looper.

Now for the notification you received from Android:

When you create a handler in a thread, it will be bound to the thread in which it is created, and when you send runnable using this handler, the code runs in the handler thread.

So, when the system saw that you want to run some code (especially 100 ms in the future) on a handler that is associated with a thread that is about to die as soon as it finishes calling the post method, he suggested using Looper.loop() to prevent terminating this thread and so that you run the second Runnable correctly in the still existing thread.

+4
source

I find the following tutorial very helpful in understanding the looper concept.
Entry into the loop and handler

+1
source

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


All Articles