Question

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?

Was it helpful?

Solution

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
        }

    }

:)

OTHER TIPS

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top