Below is a snippet from google
http://code.google.com/android/kb/commontasks.html#threading
public class MyActivity extends Activity {
[ . . . ]
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
[ . . . ]
}
protected void startLongRunningOperation() {
// Fire off a thread to do some work that we shouldn't do
directly in the UI thread
Thread t = new Thread() {
public void run() {
mResults = doSomethingExpensive();
mHandler.post(mUpdateResults);
}
};
t.start();
}
private void updateResultsInUi() {
// Back in the UI thread -- update our UI elements based on
the data in mResults
[ . . . ]
}
}
As per the best practice, we are supposed to update UI components via
handler as shown above.
My question, the Runable thread mUpdateResults can update UI directly
but not the thread created in startLongRunningOperation..
For eg. can we rewrite the code as follows:
protected void startLongRunningOperation() {
// Fire off a thread to do some work that we shouldn't do
directly in the UI thread
Thread t = new Thread() {
public void run() {
mResults = doSomethingExpensive();
updateResultsInUi(); // updating UI here.....
}
};
t.start();
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---