27

Just clarifying but in an Android activity on MAIN Thread if I call Looper.myLooper() vs Looper.getMainLooper() the return the same reference, right? they are the same thing? I know I would never have to call these usually as Android takes care of this but I'd like to know how they differ when being called from the main thread?

if from the main thread I call

Looper.myLooper().quit();
// or
Looper.getMainLooper().quit();

They both give the same runtime exception so I'm assuming they are the same reference:

Caused by: java.lang.RuntimeException: Main thread not allowed to quit.

can anyone confirm?

Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
j2emanue
  • 60,549
  • 65
  • 286
  • 456

3 Answers3

39

You have it described in the docs:

getMainLooper()

Returns the application's main looper, which lives in the main thread of the application.

myLooper()

Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper.

As for whether getMainLooper() is of any use, I can assure you it really is. If you do some code on a background thread and want to execute code on the UI thread, e.g. update UI, use the following code:

new Handler(Looper.getMainLooper()).post(new Runnable() {
  // execute code that must be run on the UI thread
});

Of course, there are other ways of achieving that.

Another use is if you want to check if the currently executed code is running on the UI thread, e.g. you want to throw/assert:

boolean isUiThread = Looper.getMainLooper().getThread() == Thread.currentThread();

or

boolean isUiThread = Looper.getMainLooper().isCurrentThread();
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • i tried this, and it works. Excellent idea to pass a queue for the handler to send messages to, instead of using default one. I never saw the constructor had those overloads. – j2emanue Dec 16 '15 at 22:27
9

Looper.getMainLooper() is convenience API to get looper which is attached to the main thread of the activity.It is usefull when you want to excute some code on main thread from a background thread.

It is usually used as follows:

new Handler(Looper.getMainLooper()).post(task);

Looper.myLooper() is api to get looper attached to current thread

rupesh jain
  • 3,410
  • 1
  • 14
  • 22
  • this makes perfect sense. thank you. getMainLooper would be used from another thread so i can gain reference to main threads looper. makes sense. I actually dont see how its useful though. The looper on androids main thread is already prepared. Very odd convenience API. – j2emanue Dec 16 '15 at 21:38
  • 1
    I have used it...suppose you are building a service which runs on secondary thread...but when you give callback to activity you want it to be on main because activity will probably want to do a UI update. In that scenarios this mainlooper() is useful.. – rupesh jain Dec 16 '15 at 21:53
3

If you call these two methods in the main thread, they are the same object! You can find answers in the source code of ActivityThread.java, Looper.java and ThreadLocal.java.

Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
ttdevs
  • 101
  • 2