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

  • SEARCH
  • Home
  • 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 8232393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:49:26+00:00 2026-06-07T17:49:26+00:00

I have a Singleton Data class, which I use to store data. I’m accessing

  • 0

I have a Singleton Data class, which I use to store data. I’m accessing it in different Fragments.

When the first Fragment is loaded, it’s no problem that all fields in the Singleton are null. When the second Fragment is shown, it depends on these fields to show its data. The first Fragment ensures these fields are initialized.

However, when the user presses the home button in the second Fragment, and opens it again after like an hour or so, the Singleton has lost all its data, and the Fragment tries to access null fields.

I wanted to implement the onSaveInstanceState method, but I’m stumped on how this works – I have no instance of the data to assign it to.

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("DATA", Data.getInstance());
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    savedInstanceState.getSerializable("DATA"); //What to do with this?
}

Any help is welcome.

  • 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-07T17:49:27+00:00Added an answer on June 7, 2026 at 5:49 pm

    Are the fragments both hosted in the same Activity? If so, why not store the shared state in a member of the Activity, and use the Activity’s onSaveInstanceState() and onCreate() to save and restore it. In your fragments you can then do…

    ((MyActivityClass)getActivity()).getSharedState()
    

    Otherwise, you could make your singleton object manage a member object that can be serialized and deserialized:

    MySingleton.instance().saveTo(outState);
    MySingleton.instance().restoreFrom(savedInstanceState);
    MySingleton.instance().getState();
    

    Where

    public void restoreFrom(Bundle savedInstanceState) {
        mState = savedInstanceState.getSerializable("DATA");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton class which manages data and database handling of my application.
I have a simple question. I use a singleton which implements an abstract class.
I have created a singleton class to keep track of my data on my
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
I have a singleton class which is being used throughout the app. I am
I have a Singleton class which is highly dependent on the XML file it
I have an Addin for MS Excel which needs a singleton to share data
I have a singleton class for global access to config information. This singleton class
I have a Singleton that is accessed in my class via a static property
I have a singleton object that use another object (not singleton), to require some

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.