user1284566
user1284566

Reputation: 167

Android - post Runnable to UI thread

If I am not mistaken, you can find out if the current thread is the UI thread by doing:

if (Looper.myLooper() == Looper.getMainLooper())
    /* we are in the UI thread */

How can I post a task to the UI thread/queue?

if (Looper.myLooper() == Looper.getMainLooper())
    X.execute(new Runnable() { ... });

Here, X is the queue/handler of the UI Thread. What is X, and can get I get it statically?

I would like to send this task without being forced to have a reference to my activity or anything else. Is this possible?

Thanks!

Upvotes: 0

Views: 2343

Answers (2)

43matthew
43matthew

Reputation: 982

I would probably recommend looking at context.runOnUiThread(Runnable r); No need to check the looper - the runOnUiThread(...) function will do that for you.

Upvotes: 1

user1284566
user1284566

Reputation: 167

new Handler(Looper.getMainLooper())

Upvotes: 2

Related Questions