How to detect UI thread on Android?

Android

Android Problem Overview


Is there a robust way to detect if Thread.currentThread() is the Android system UI thread in an application?
I would like to put some asserts in my model code that asserts that only one thread (eg the ui thread) accesses my state, to assure that no kind of synchronization is necessary.

Android Solutions


Solution 1 - Android

Common practice to determine the UI Thread's identity is via Looper#getMainLooper:

if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
  // On UI thread.
} else {
  // Not on UI thread.
}

From API level 23 and up, there's a slightly more readable approach using new helper method isCurrentThread on the main looper:

if (Looper.getMainLooper().isCurrentThread()) {
  // On UI thread.
} else {
  // Not on UI thread.
}

Solution 2 - Android

I think that best way is this:

 if (Looper.getMainLooper().equals(Looper.myLooper())) {
     // UI thread
 } else {
     // Non UI thread
 }

Solution 3 - Android

As of API level 23 the Looper has a nice helper method isCurrentThread. You could get the mainLooper and see if it's the one for the current thread this way:

Looper.getMainLooper().isCurrentThread()

It's practically the same as:

Looper.getMainLooper().getThread() == Thread.currentThread()

but it could be a bit more readable and easier to remember.

Solution 4 - Android

public boolean onUIThread() {
return https://developer.android.com/reference/android/os/Looper.html">Looper</a>.<a href="https://developer.android.com/reference/android/os/Looper.html#getMainLooper()">getMainLooper()</a>.<a href="https://developer.android.com/reference/android/os/Looper.html#isCurrentThread()">isCurrentThread()</a>;;




}

}

But it requires API level 23

Solution 5 - Android

Besides checking looper, if you ever tried to logout thread id in onCreate(), you could find the UI thread(main thread) id always equals to 1. Therefore

if (Thread.currentThread().getId() == 1) {
    // UI thread
}
else {
    // other thread
}

Solution 6 - Android

Nice extension for Kotlin:

val Thread.isMain get() = Looper.getMainLooper().thread == Thread.currentThread()

So you just call:

Thread.currentThread().isMain

Solution 7 - Android

Couldn't you use the runOnUiThread method in the Activity class?See..

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParDroidView Question on Stackoverflow
Solution 1 - Androidmik3yView Answer on Stackoverflow
Solution 2 - AndroidATomView Answer on Stackoverflow
Solution 3 - Androidstan0View Answer on Stackoverflow
Solution 4 - AndroidcmicatView Answer on Stackoverflow
Solution 5 - AndroidyushulxView Answer on Stackoverflow
Solution 6 - AndroidRenetikView Answer on Stackoverflow
Solution 7 - AndroidanonView Answer on Stackoverflow