0

I have trouble to update the UI in activity when handler has called in other class. Below is the code that I created.

Activity A

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_A);

    Handler handler = new Handler(); //Here is where the handler is start, other class will update the Handler message.
    ......
}

Activity B

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_B);

    ......
}

//Just a dummy function called when handler is triggered
private void onChange(){
    TextView tv_status = findViewById(R.id.status);
    tv_status.setText("Complete");
}

Class Handler

public Handler {

    public Handler(){
         Handler handler = new Handler(){
               public void HandleMessage(Message msg){
                    switch (msg.what) {
                        case 1:
                             break;
                        case 2:
                             //should trigger to update the TextView in Activity B here
                             break;
                   }
              }
        };

        .......

    }
}

Activity A as main where the Handler is called to start the Handler, Activity B is just update the UI triggered by Handler.

Siti Aishah Ismail
  • 639
  • 1
  • 13
  • 31

1 Answers1

0

Hope this will help.

public Handler {

    public Handler(){
         Handler handler = new Handler(){
               public void HandleMessage(Message msg){
                    switch (msg.what) {
                        case 1:
                             break;
                        case 2:
                             //should trigger to update the TextView in Activity A here 
                            runOnUiThread(new Runnable() {
                             @Override
                               public void run() {
                                 // Update your view
                               }
                              });
                             break;
                   }
              }
        };

        .......

    }
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50