Updating Android UI Using Threads

Updating Android UI using threads

You cannot touch anything in the UI thread from a background thread, to do that use Handlers, initialize your background thread passing it a Handler object. When data arrives to use the handler to send a message to the UI. In the UI when the message from the background thread comes, just update the Views.

Example Code Snippet :

in the background thread:

if(dataArrives){
Message msg = handler.obtainMessage();
msg.what = UPDATE_IMAGE;
msg.obj = bitmap;
msg.arg1 = index;
handler.sendMessage(msg);
}

in the UI thread:

final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==UPDATE_IMAGE){
images.get(msg.arg1).setImageBitmap((Bitmap) msg.obj);
}
super.handleMessage(msg);
}
};

and pass the handler to the background thread.

Problem with updating UI with background thread

Undoubtedly, you should use different thread for heavy processes in java. But, Anything related to UI changes, initializations like Views, Animations should be done in UI(Main) Thread which is created with your application.

You could also do:

 new Thread(new Runnable() {
@Override
public void run() {
final Bitmap bitmap =
processBitMap("image.png");

new Handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});


}

Even if you do a background job in another thread, Handler guarantees, setting image is done in UI/Main thread.

Android multi threading UI update

I found a solution based on both of your answers :) Thank you

MainActivity class:

public static Handler UIHandler;

static {
UIHandler = new Handler(Looper.getMainLooper());
}

public static void runOnUI(Runnable runnable) {
UIHandler.post(runnable);
}

receive class

MainActivity.runOnUI(new Runnable() {
public void run() {
MainActivity.textViewState.setText(message_received);
}
});

Why is it possible to update UI using Handler created in a background Thread?


When you connect a Handler to your UI thread, the code that handles
messages runs on the UI thread.

taken from the docs

i believe because you're calling the workerThread.startTask() from the button click, which is the UI thread, the handler is simply returning the result to the UI thread as well, which is why the UI will update as well

Update Android UI from a thread in another class


What I'm trying to do is update the UI from a thread which has been
created in another class. I've seen all of the suggestions, such as
async, handlers, runnable, etc... but I've having real trouble
implementing them in separate classes.

Generally for your goal i recommend to you use:

  • AsyncTask
  • IntentService with ResultReceiver

I don't think that its too tricky. Absolutely not. If you have it as separated class(es) and not as inner class(es) in some Activity class so i recommend to use constructor where you will pass context, widgets, generally whatever you want and then in correct methods(which allows UI update) update your UI.

I'm doing it because i like when i have clean classes(so UI class have only UI implementations and logic is positioned separately).

Example:

public class TaskExample extends AsyncTask<Void, Integer, Void> {

private Context c;
private Button b;

public TaskExample(Context c, Button b) {
this.c = c;
this.b = b;
}

protected Void doInBackground(Void... params) {
// some work
if (isSomethingConnected) {
publishProgress(Constants.IS_CONNECTED);
}
return null;
}

public void onProgressUpdate(Integer... params) {
switch (params[0]) {
case Constants.IS_CONNECTED:
b.setText("Connected");
break;
case Constants.ANOTHER_CONSTANT:
// another work
break;
}
}
}

Usage:

public class Main extends Activity implements View.OnClickListener {

private Button b;

public void onCreate(Bundle b) {
super.onCreate(b);
// initialise widgets and set listeners to appropriate widgets
}

public void onClick(View v) {
switch(v.getId()) {
case R.id.connectBtn:
startWorker();
break;
}
}

private void startWorker() {
TaskExample te = new TaskExample(this, b);
te.execute();
}
}


Related Topics



Leave a reply



Submit