Issue
Given that posting a task with post()
puts the Runnable
in a queue, is there any difference between
protected void onCreate(Bundle savedInstanceState) {
Log.d("UI thread", "Do something");
}
and
protected void onCreate(Bundle savedInstanceState) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d("UI thread", "Do something");
}
});
}
?
In both cases, there should only be one thread running and no concurrency happening - right?
Then what's the benefit in creating a handler that attaches to the UI thread and running tasks on it?
Solution
The timing is different. In the first snippet the code is executed as part of the onCreate
execution so it is guaranteed to finish before onCreate
returns, in the second snippet, it is executed some time later (maybe after several other callbacks).
Answered By - Henry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.