android newbie
android newbie

Reputation: 11

Android ListFragment Cursor managing

I want to use ListFragment and Loader Class for my android app. (honeycomb level SDK, but Google release comparability package)

in Fragment, is it has managing cursor API with Fragmemt life cycle

Activity has "startManagingCursor(Cursor c)"

or

Fragment automatically manage cursor in its life cycle?

Upvotes: 1

Views: 3173

Answers (1)

ptashek
ptashek

Reputation: 186

I'm not sure I got your question right, but if you're asking if your cursor is managed automaticaly in a ListFragment when using a Loader, then I believe the answer is: yes, it is.

In your ListFragment you would implement the LoaderManager.LoaderCallbacks interface, and then use initLoader() and restartLoader() from LoaderManager to manage your data and the cursor during the ListFragment's lifecycle. The underlying adatpter must support the swapCursor() method for this to work best.

Something along the lines of:


class Foo extends ListFragment implements LoaderManager.LoaderCallbacks {

    private MyAdapter   mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter = new MyAdapter();
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        Uri MY_URI = "your data URI goes here";
        return new CursorLoader(getActivity(), MY_URI, MY_PROJECTION, selection, selectionArgs, sortOrder);
    }

    @Override
    public void onLoadFinished(Loader loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader loader) {
        mAdapter.swapCursor(null);
    }
}

Upvotes: 2

Related Questions