Когда я обновляю View в другом потоке (через View.post(Runnable)), переданное сообщение может не прийти, например, как в следующем коде:
Код: Выделить всё
public class MainActivity extends Activity {
private TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
output = new TextView(this);
setContentView(output);
new Thread(new Runnable() {
@Override
public void run() {
try {
appendText("A");
Thread.sleep(100);
appendText("B");
} catch (Exception e) {
appendText(e.toString());
}
}
}).start();
}
private void appendText(final Object msg) {
output.post(new Runnable() {
@Override
public void run() {
output.setText(output.getText() + "\n" + msg);
}
});
}
}Код: Выделить всё
Thread.sleep(100);Код: Выделить всё
Thread.sleep(100);Код: Выделить всё
appendText("A");Почему так происходит?
