是onLoadFinished()异步(后台线程)?

Obi*_*MD5 8 android android-loadermanager

我目前正在寻找使用装载程序管理器在drawerlayout中填充我的expandablelistview.我不能在文档中发现任何地方如果在回调函数onLoadFinished()在UI线程或在后台线程上运行.它是在后台线程吗?

Nik*_*ski 6

如果你init()从UI线程调用,onLoadFinished()肯定会在UI线程上调用.例如,当您从后台调用时AsyncTaskLoader,将通知结果的线程将是您初始化加载器的线程.

......但你仍然可以做到以下几点:

@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
        }

    }
Run Code Online (Sandbox Code Playgroud)

:)