java 调用 initLoader() 后加载程序没有启动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10321712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:33:47  来源:igfitidea点击:

Loader doesn't start after calling initLoader()?

javaandroidandroid-activityandroid-fragmentsandroid-loadermanager

提问by user291701

I have a fragment, and want to start a loader when a button is clicked:

我有一个片段,想在单击按钮时启动加载程序:

public class MyFragment extends Fragment {

    public void onActivityCreated() {
        super.onActivityCreated();

        Button btn = ...;
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                getLoaderManager().initLoader(500, null, mMyCallback);
            }
        });
    }  

    private LoaderManager.LoaderCallbacks<String> mMyCallback = new  LoaderManager.LoaderCallbacks<String>() {

        @Override
        public Loader<String> onCreateLoader(int arg0, Bundle arg1) {
            Log.e(TAG, "LoaderCallback.onCreateLoader().");
            return new MyLoader(getActivity());
        }
    }
}

public class MyLoader extends AsyncTaskLoader<String> {
    public MyLoader(Context context) {
        super(context);
    }

    @Override
    public String loadInBackground() {
        Log.e(TAG, "Hi, running.");
        return "terrific.";
    }
}

After clicking the button, I can see my callback's onCreateLoader method called, but the created loader never actually starts. Do we need to call forceLoad() on the loader itself to get it to actually start? None of the sample posts do this,

单击按钮后,我可以看到调用了回调的 onCreateLoader 方法,但创建的加载程序从未真正启动。我们是否需要在加载器本身上调用 forceLoad() 才能让它真正启动?没有一个示例帖子这样做,

Thanks

谢谢

回答by Alex Lockwood

You need to implement onStartLoading()and call forceLoad()somewhere in the method.

您需要在方法中的某处实现onStartLoading()和调用forceLoad()

See this post for more information: Implementing Loaders (part 3)

有关更多信息,请参阅此帖子:实现加载程序(第 3 部分)

回答by nohawk

In my experience it never worked unless I used forceLoad().

根据我的经验,除非我使用forceLoad().

You may find the answer to this previous question helpful: Loaders in Android Honeycomb

您可能会发现上一个问题的答案很有帮助: Android Honeycomb 中的加载程序

回答by Tarun

Three important points regarding Loaders are:

关于 Loader 的三个要点是:

  1. Always Use forceLoad()method while initialising Loaders. For Example:

    getLoaderManager().initLoader(500, null, mMyCallback).forceLoad();
    
  2. Always implement onStartLoading(). This function will automatically be called by LoaderManagerwhen the associated fragment/activity is being started.

  3. Make sure the ID of the loader is unique otherwise new Loader will not be called.

  1. forceLoad()初始化加载器时始终使用方法。例如:

    getLoaderManager().initLoader(500, null, mMyCallback).forceLoad();
    
  2. 始终执行onStartLoading()LoaderManager当相关的片段/活动正在启动时,将自动调用此函数。

  3. 确保 loader 的 ID 是唯一的,否则将不会调用新的 Loader。

If there is still a problem you can check the state of loader by calling the isStarted()method.

如果仍然有问题,您可以通过调用该isStarted()方法来检查 loader 的状态。

回答by Snicolas

You need to keep a reference to the instance of the loader you create in the method onCreateLoader. Then, to refresh it, call yourLoader.onContentChanged();

您需要保留对您在方法中创建的加载程序实例的引用onCreateLoader。然后,要刷新它,请调用yourLoader.onContentChanged();

回答by Golan Shay

If you have more than 1 loader in the same activity, make sure their id differs. I lost few hours to figure it out :)

如果您在同一活动中有 1 个以上的加载程序,请确保它们的 ID 不同。我花了几个小时才弄清楚:)