Sam YC
Sam YC

Reputation: 11647

Similar function SwingUtilities.invokeLater in android?

Sometime I feel that using AsyncTask is quite overkill for the task, I am looking for similar function like SwingUtilities.invokeLater in android. Because I just want to execute one line of code, no point to create a new class for that.

Upvotes: 15

Views: 5496

Answers (3)

Ahmed
Ahmed

Reputation: 15069

Activity.runOnUiThread(Runnable action);

The above method is slightly different than invokeLater of java because it checks if you are already on ui thread then it will run your Runnable immediately.

There is another way to make sure your Runnable gets called later and not immediately even if you are on UI thread. To do that you write following code while you are on the main thread.. if you call this when on backend thread your runnable will be exected on backend thread..

Handler h = new Handler();
h.post(new Runnable() {
    @Override
    public void run() {
        //your code..
    }
);

Upvotes: 6

tenorsax
tenorsax

Reputation: 21243

Maybe you're looking for:

Activity.runOnUiThread(Runnable action)

Upvotes: 24

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52966

I don't remember exactly what invokeLater() does, but if you want to execute something delayed, you can use postDealayed(). That goes on the UI thread, so not fit for long running tasks, naturally.

Upvotes: 1

Related Questions