Android Snippet. How to quickly run code on the main thread

Here’s a simple code snippet that enables you to run a Runnable object on the main thread, or UI thread. This is very handy if you have to update your UI from a background thread or an AsyncTask. It does not require something in particular, this code can be run from everywhere inside your application:

new Handler(Looper.getMainLoooper()).post(new Runnable() {
    @Override
    public void run() {
        // your code here
    }
}

Enjoy 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: