I am currently looking at using a loader manager to populate my expandablelistview in a drawerlayout. I cannot find anywhere in the documentation if the callback function onLoadFinished() is running on the UI thread or on a background thread. Is it on a background thread?
For example, if your app makes a network request from the main thread, your app's UI is frozen until it receives the network response. You can create additional background threads to handle long-running operations while the main thread continues to handle UI updates.
Which method is used to set the updates of UI? Android Thread Updating the UI from a Background Thread The solution is to use the runOnUiThread() method, as it allows you to initiate code execution on the UI thread from a background Thread.
User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.
Service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise.
If you have called init()
from the UI thread, onLoadFinished()
will definitely be called on the UI thread. In cases when you call from background for example an AsyncTaskLoader
the thread that will be notified about the outcome will be the thread from where you init loader.
...But you still can do the following:
@Override
public void onLoadFinished(Loader<String> arg0, String arg1) {
Runnable populate = new Runnable(){
@Override
public void run() {
//your code
}
};
if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
//on Ui thread
populate.run();
}else{
this.runOnUiThread(populate); //or use handler to run the runnable
}
}
:)
http://www.amazon.com/Android-Programming-Ranch-Guide-Guides/dp/0321804333, pg. 566.
"The onFinishedLoad() method will be called on the main thread once the data has been loaded in the background."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With