SherlockFragmentActivity with multiple ListFragments and SQLite cursor battles

99封情书 提交于 2019-12-05 21:19:16

The issue is that the first parameter of the initLoader method is a way to uniquely identify each item to be loaded, all of mine were set to the same (0) value -- changing them to a unique value fixed the issue.

The problem is that your Fragments are directly manipulating the Activity's LoaderManager, and as a result the first Loader with ID 0 is always being reused.

Each Activity and Fragment in your app has its own LoaderManager instance, so instead of manipulating the Activity's LoaderManager directly from the Fragment, you should have your Fragment make use of its own LoaderManager. That is, instead of

// initialize a new Loader that will be managed by the Activity's LoaderManager
getActivity().getSupportLoaderManager().initLoader(0, null, this);

you should have each individual Fragment implement the LoaderCallbacks, and then in onActivityCreated, have each one call,

// initialize a new Loader that will be managed by the Fragment's LoaderManager
getSupportLoaderManager().initLoader(0, null, this);

In the documentation on Fragments, one of the things they stress the most is that Fragments should be designed for reuse and shouldn't be specific to a particular Activity. Thus, your "hack" (i.e. supplying different Loader ids for each Fragment) is technically "bad practice" because your Fragments will only work correctly when attached to that specific Activity. You want your Fragments to work with any Activity, so you usually want to pack your Fragments with as much control over its own UI as it can.

For me in the class which is

    public abstract class AbstractListFragment extends SherlockFragment implements
    LoaderCallbacks<Cursor> 

the problem with not loading content was fixed by commenting one 1st line of code and adding the second one:

    // getActivity().getSupportLoaderManager().initLoader(0, null, this);
    getLoaderManager().initLoader(0, null, this);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!