简体   繁体   中英

Restoring Fragment Instance with references

Short version: I would like to know how i can recreate a fragment state (eg after screen-rotation) when this fragment contains a reference to an object that cannot be serialized or duplicated and needs to stay in memory.

Long version: In my app i use a ViewPager connected to a custom FragmentPagerAdapter that instantiates a number of fragments which display data (a schedule) contained in a ListView. All this is contained inside a parent fragment. When instantiated by the adapter, each page fragments is passed a reference to one single object (the " ScheduleManager ") that does several things:

  • Contains the data to be displayed
  • Holds a reference to a Context object (in order to access SharedPreferences )
  • Holds a reference to the parent fragments LoaderManager so it can reload the data
  • Implements OnClick - and ActionMode callback listeners (to be able to create and handle an action mode that works across all pages; Page Fragments add the object as a listener to their ListViews)
  • Defines a callback interface to notify listeners of state changes or when data is reloaded (Page Fragments register themselves as listeners).

Essentially, the ScheduleManager holds everything together and implements the main logic of this ("schedule") part of my app, ie loading and providing the data, and the means to modify and relaod it via an ActionMode . I don't know whether this is good design...

My question is how am I supposed restore the Fragments instance state under these circumstances? I cannot serialize the ScheduleManager to a bundle, since it would loose its references to the Context and the LoaderManager (otherwide, of course I would use setArguments / getArguments ). Also, all the page fragments must have a reference to the same instance of the ScheduleManager , otherwise the shared action mode won't work. Aside from that I don't want to duplicate the entire schedule data each time a fragment is restored. I want to keep this object in memory and let the page fragments reclaim a reference to it when they are restored.

I guess I could let the containing activity hold the ScheduleManager and have the PageFragment query it for a reference. But I would prefer to keep the everything inside the parent fragment self-contained and modular if this is possible (there can be different schedules for different items). While writing this though I get the feeling there will be no way around this.

Of course, when the parent fragment is recreated it can also recreate the ScheduleManager and feed to it the references it needs ( Context + LoaderManager ). The problem is there can be no more than one instance of ScheduleManager for each instance of the parent fragment, so how to make the nested page fragments reconnect to it.

Here is what I ended up doing:

public void onCreate(Bundle savedInstanceState)
{
    Fragment fragment = this.getActivity().getFragmentManager().findFragmentByTag("SCHEDULE");
    if (fragment instanceof ScheduleMasterFragment)
    {
        ScheduleMasterFragment master = (ScheduleMasterFragment) fragment;
        this.scheduleManager = master.getScheduleManager();
    }
    else
    {
        throw new RuntimeException("SchedulePageFragment must be " +
                "the child of a ScheduleMasterFragment with Tag set to 'schedule'");
    }
    super.onCreate(savedInstanceState);
}

The ScheduleManager is the reference I wanted to keep. At least this way I keep all the code inside the fragment itself, except for getScheduleManager in ScheduleMasterFragment which makes sense because the ScheduleMasterFragment somehow is the owner of the ScheduleManager instance.

Any better solutions are still welcome...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM