Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is onLoadFinished() asynchronous (background thread)?

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?

like image 819
ObieMD5 Avatar asked Aug 20 '13 03:08

ObieMD5


People also ask

What is background thread in Android?

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.

What are different ways of updating UI from background thread?

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.

What is Android UI 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.

Does Android service run on main 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.


2 Answers

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
        }

    }

:)

like image 182
Nikola Despotoski Avatar answered Oct 27 '22 12:10

Nikola Despotoski


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

like image 37
dpott197 Avatar answered Oct 27 '22 12:10

dpott197