0

I viewed the answer here: Updating Android UI using threads

But I am not able to understand how to instantiate the handler in the background thread to match the UiThread.

I just want to be clear and say that im using are 2 entirely separate classes.

UiThread Handler Code:

      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);
      }
    };

Background Thread Handler code:

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

In the background class, im getting "handler" as undefined. Please show the entire thread classes in your answer if you can.

Community
  • 1
  • 1
  • You just need to add a Handler to your constructor for your background thread that you pass and store a reference to so you can utilize it in the background thread. – zgc7009 Sep 22 '16 at 16:12
  • You mean the handler as a parameter? – DavidPrabhu Sep 22 '16 at 17:45
  • Correct, store a local reference in your thread to the handler passed as a parameter in the constructor and you can use it from there. – zgc7009 Sep 22 '16 at 19:23

0 Answers0