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?
Is onLoadFinished() asynchronous (background thread)?
Asked Answered
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
}
}
:)
When I added the check to see if the callback is in MainThread this is what I am seem to getting.. "Init not in Main thread","Finished in Main Thread". <p> Please note that I had called the Loader from a separate thread. Does this mean no matter how you call init loader, your onLoadFinished gets called in UI thread ? –
Featherstone
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."
Add code below will solve the prob that calling getView() in onLoadFinished() returns NullPointerException
@Override
public void onStop() {
super.onStop();
getSupportLoaderManager().destroyLoader(LOADER_ID);
}
© 2022 - 2024 — McMap. All rights reserved.