2

Android can't update view direct on non-ui thread,but if I just read/get some information for ui?

For example I have a method updateModel() like

void updateModel() {
    dailyReport.log.setValue(editLog.getText().toString());
    dailyReport.plan.setValue(editPlan.getText().toString());
    dailyReport.question.setValue(editQuestion.getText().toString());
}

Is it a problem if I run this method on non-ui thread.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Eason Zhu
  • 23
  • 2

3 Answers3

1

Example below helped me solve this problem. Hope this will help you too

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //do your job
        }
    });
Ragaisis
  • 2,700
  • 1
  • 26
  • 41
0

Yes you can Update the UI from a Non UI thread. There are two ways of doing this.

1) Do this with activity object (easy option get and Update)

2) Do this using Message Handler (a bit difficult and Update only)
Going for the 1st one all you need to do is Pass the Activity into the constructor. Here is Thread snippet

    public class MyThread extends Thread {

        private Activity _activity;

        public MyThread(Activity activity) {
            this._activity = activity;

        }
    @Override
    public void run() {
        super.run();
           //do all what you want. and at the end to Update the Views Do this
           if(!this._activity.isFinishing())
           { // executed if the activity is not finishing
           this._activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //set the public variables UI Variables like this
    dailyReport.log.setValue(this._activity.editLog.getText().toString());
    dailyReport.plan.setValue(this._activity.editPlan.getText().toString());
    dailyReport.question.setValue(this._activity.editQuestion.getText().toString());
         });

    }
 }
}

Now in the Activity

MyThread thread = new MyThread(this);
thread.start;
Quamber Ali
  • 2,170
  • 25
  • 46
0

Is it a problem if I run this method on non-ui thread?

With the assumption that dailyPlan is a model class and its methods do not modify the UI, then no, it is not a problem, Android will not complain and you will not receive any runtime errors. However, I would not follow this approach as in general it's a bad practice to access directly one threads data from another thread - you never know who is modifying what, read/write issues can occur and so on. These are usually solved by synchronizing the data, but if you put synchronized code in UI thread you made things even worse!

For your kind of problem, why don't you pass the data from UI controls to the thread that uses above logic? When you create it, pass the 3 strings:

  1. editLog.getText().toString()
  2. editPlan.getText().toString()
  3. editQuestion.getText().toString()

Example:

private EditText editLog;
private EditText editPlan;
private EditText editQuestions;

private void activityMethodThatStartsThread() {
    String log = editLog.getText().toString();
    String plan = editPlan.getText().toString();
    String questions = editQuestions.getText().toString();
    DailyReportModel model = new DailyReportModel(log, plan, questions);
    model.start();
}

public class DailyReportModel extends Thread {
    private String log;
    private String plan;
    private String questions;

    public DailyReportModel(String log, String plan, String questions) {
        super();
        this.log = log;
        this.plan = plan;
        this.questions = questions;
    }




    void updateModel() {
        dailyReport.log.setValue(log);
        dailyReport.plan.setValue(plan);
        dailyReport.question.setValue(questions);
    }
}
gunar
  • 14,660
  • 7
  • 56
  • 87