Search code examples
androidandroid-fragmentsandroid-contentproviderandroid-cursorloader

How to share cursor between fragments/activities


I'm trying to build an application that will have list/detail panes built with fragments. List is created from the ContentProvider. When clicked on the list the details is populated or activity created. In action similar to gmail application. How the data should be shared/passed between fragments/activities?

Population of the list wasn't that hard, but how can I pass the selection to the details fragment (either in the same activity or another activity)? Should I query again and returned result used in details?

It needs to be like gmail app, so swipe left/right should then change the details accordingly to the same order as the list is either in the single pane layout or dual pane layout.

I think for this I need to share the Cursor returned by the CursorLoader to keep the same order. Then swipes would increase/decrease the index and would display correct item.

Also as I already loaded data, I wanted to reuse it without new queries.

Could someone point me to the right direction, what you would you do to achieve that (no code but algorithm/steps)?

At the moment I have 2 activities list and detail, the list has dual and single panel layout and list fragment with details fragment are used, detail has just single pane with details fragment. I think I could reduce it to be a single activity and juggle the fragments but don't know if it will be good.


Solution

  • What I did then was:

    activity holding the fragment(s) will do:

    1. implement LoaderManager.LoaderCallbacks<Cursor>
    2. call the getSupportLoaderManager().initLoader(ALL_DATA_LOADER_ID, null, this);
    3. pass arguments of the LoaderCallbacks to the fragments that also implements LoaderCallbacks
    4. call the getSupportLoaderManager().restartLoader(ALL_DATA_LOADER_ID, null, this); whenever data was changed outside of the ContentProvider means

    This way I'm sharing the same cursor between fragments. Between activities I'm exchanging (via Intent) the data needed to request the same dataset and selection id if needed.