Recreate cursorloader for fragment

2.1k Views Asked by At

In app activity I have ViewPager attached to FragmentPagerAdapter which have three items all of them have list views attached to cursor loaders.

public static class TaskListFragment extends Fragment implements LoaderCallbacks<Cursor> {          
    public TaskListFragment() {     
    }
    protected static final String excludeCompletedArg = "excludeCompleted";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...         
        getLoaderManager().initLoader(filter, cursorArgs, this);
...         
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        // TODO Auto-generated method stub

        return new CursorLoader(getActivity(), 
                Uri.parse("content://someurl"), 
                null, 
                null, 
                new String[] { Integer.toString(arg0), arg1.getString(TaskListFragment.excludeCompletedArg) }, 
                null);
    }
}

But which way I can force to reload cursor with new bundle argument? For an example I handle onTabReselected of main activity

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    LoaderManager loaderManager = mSectionsPagerAdapter.getItem(tab.getPosition()).getLoaderManager();      
}

thows exception.

I don't want to use FragmentStatePagerAdapter instead.

3

There are 3 best solutions below

0
On
    public static class TaskListFragment extends Fragment implements LoaderCallbacks<Cursor> {
        private static Hashtable<Integer, LoaderCallbacks<Cursor>> loadedFragments = null;

    public static void ReloadCursor(int fragmentIndex) {
        Bundle cursorArgs = new Bundle();
        cursorArgs.putString("someArg", "someArgNewValue");

        ((Fragment) loadedFragments.get(fragmentIndex)).getLoaderManager().restartLoader(fragmentIndex, cursorArgs, loadedFragments.get(fragmentIndex));
    }

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ...

        if (loadedFragments == null) {
            loadedFragments = new Hashtable();
        }

        Bundle cursorArgs = new Bundle();
        cursorArgs.putString("someArg", "someArgValue");

        getLoaderManager().initLoader(fragmentIndex, cursorArgs, this);

        loadedFragments.put(fragmentIndex, this);

                    ...
    }
}

Then invoke TaskListFragment.ReloadCursor(int fragmentIndex) from parent activity, for example, from onTabReselected. It works.

Any suggestions for more simple way?

3
On

I assume you whant to reload cursor in some situations. In order to do that you may use restartLoader method. In your method onTabReselected you just need to restart loader of given id and pass in the argument to recreate.

getLoaderManager().restartLoader(id, args, this);

It works just like initLoader but always enforces reload. Your onCreateLoader pattern should be:

@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
   if (loaderId == ARBITRARY_LOADER_ID) {
     // create and return Cursor Loader
   }
}

Refer: http://developer.android.com/reference/android/app/LoaderManager.html http://developer.android.com/guide/components/loaders.html

0
On

There are two ways to go about it, 1) if the Loader is on an Activity, just restart loader. e.g getLoaderManager().restartLoader(id,bundle,callback);

2) if the Loader is on a Fragment, restart the fragment from its host Activity, by creating an interface on the fragment to send it details to the Activity and restart fragment if details is true.