I am confused as to why updating an ui element in a handler, asynctask, or runOnUiThread IF YOU ARE ON UI THREAD ALREADY consider the following snippets, numbered from 1 to 4: (for demo purpose, might have syntax error)
// in MainActivity.java #1
public void onCreate(Bundle bundle) {
setContentView(R.layout.main);
TextView name = (TextView)findViewById(R.id.name);
name.setText("Name Changed!!");
}
// in MainActivity.java #2
public void onCreate(Bundle bundle) {
setContentView(R.layout.main);
TextView name = (TextView)findViewById(R.id.name);
handler.post(new Runnable() {
public void run() {
name.setText("Name Changed!!");
}
});
}
// in MainActivity.java #3
public void onCreate(Bundle bundle) {
setContentView(R.layout.main);
TextView name = (TextView)findViewById(R.id.name);
runOnUiThread(new Runnable() {
public void run() {
name.setText("Name Changed!!");
}
});
}
// in MainActivity.java #4
public void onCreate(Bundle bundle) {
setContentView(R.layout.main);
...same thing,...update textview in AsyncTask
}
// in MainActivity.java #5
public void onCreate(Bundle bundle) {
setContentView(R.layout.main);
TextView name = (TextView)findViewById(R.id.name);
name.post(new Runnable() {
public void run() {
name.setText("Name Changed!!");
}
});
}
as you can see from examples #1 - #4, i don't see why you need to use #2, #3, or #4, becuase #1 you are already on UI thread!!!
in other words, i am saying #1-#4 are the same -- i.e. you want to execute something on the UI THREAD/MAIN THREAD, so tell me why you would use #2, #3, or #4, if #1 is already on UI thread.
what is the difference between each one?
please provide any citation of documentation or real use cases
thank you!!