user3422952
user3422952

Reputation: 317

Android - updating UI while running a task?

I've asked before but received no helpful responses. I've got a method. I want to update the UI through the method. I've tried running the code on the UI thread, I've tried posting the code (.post[...]), I've tried creating threads but nothing works.

Here's a simplified version of my code:

int x;
for(int i = 0; i < times; i++){
    if((i % 2) == 1){
        x += (x / (x * 0.5));
    } else {
        x += (x / (x * 0.25));
    }
    while((System.currentTimeMillis() - t) < 2000){
        // wait
    }
    runOnUiThread(new Runnable(){
        @Override
        public void run(){
            btn.setText(x.toString());
        }
    }
} // The for loop won't update the UI until the entire method has finished.

Like I said, creating a new thread/running on UI thread/posting doesn't help. How can I update the UI while a method is running?

For some reason the UI will wait until the method has finished.

Upvotes: 1

Views: 1718

Answers (4)

ETL
ETL

Reputation: 10001

AsyncTask is deprecated in latest Android. (See AsyncTask doc).

The preferred method is now to use a Thread Executor as covered in the Android Background Execution Guide.

This is done by wrapping the task code in a Runnable and executing it with one of the many Executors available.

Upvotes: 0

cliffroot
cliffroot

Reputation: 1691

You might want to use AsynkTask here. It can be something like this.

class MyTask extends AsycnTask<Void,Integer,Void> {
    @Override
    protected Void doInBackground(Void... params) {
        for(int i = 0; i < times; i++){
            if((i % 2) == 1){
                x += (x / (x * 0.5));
            } else {
            x += (x / (x * 0.25));
            }
            publishProgress(x);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        btn.setText(String.valueOf(progress[0]));
    }
}

And then in your Activity new MyTask().execute(). More info on AsyncTask here

But does this method really causes your UI thread to freeze? Isn't it simply updating the values if you call it as is in your activity?

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006539

For some reason the UI will wait until the method has finished.

Your call to setText() schedules an update to the screen, but that update cannot occur until you return control of the main application thread to the framework. So long as you are tying up that thread, the screen will not update.

How can I update the UI while a method is running?

If that method is running on the main application thread -- as yours must be if the code snippet above is not crashing -- you cannot update the UI while it is running.

You may wish to ask a fresh Stack Overflow question where you explain what you're really trying to do. In your original question, you refer to an animation, yet you provide no code related to that animation, or your implementation of runIntensiveMethod().

Upvotes: 1

P-RAD
P-RAD

Reputation: 1341

may be you should use AsyncTask

AsyncTask has method onProgressUpdate(Integer...) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().

android docs

Upvotes: 0

Related Questions