Is onLoadFinished() asynchronous (background thread)?
Asked Answered
L

3

8

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?

Lustful answered 20/8, 2013 at 3:2 Comment(0)
F
6

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
        }

    }

:)

Frit answered 20/8, 2013 at 3:15 Comment(1)
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
R
2

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."

Ratal answered 20/8, 2013 at 3:16 Comment(0)
M
0

Add code below will solve the prob that calling getView() in onLoadFinished() returns NullPointerException

@Override
public void onStop() {
    super.onStop();
    getSupportLoaderManager().destroyLoader(LOADER_ID);
}
Martz answered 9/11, 2013 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.