Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:33:45+00:00 2026-06-18T10:33:45+00:00

Short version: I would like to know how i can recreate a fragment state

  • 0

Short version: I would like to know how i can recreate a fragment state (e.g. 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, i.e. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:33:47+00:00Added an answer on June 18, 2026 at 10:33 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version : I would like the maven-glassfish-plugin to only be executed in the
I would like to know if the app icon on the iPhone can be
I know that this probably doesn't really matter, but I would like to know
Short version Need to distribute nightly builds to 70+ people each morning, would like
Short version: Can I grant access to external databases to a role? Long version:
Short version: In pure Java EE 6, is there something like Spring's Authentication Processing
Short version : echo testing | vim - | grep good This doesn't work
Short version: How can I map two columns from table A and B if
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
Short and sweet version: Is there a single web service method that would return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.