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 424543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:17:26+00:00 2026-05-12T19:17:26+00:00

I frequently run into the problem that I have to preserve state between several

  • 0

I frequently run into the problem that I have to preserve state between several invocations of an activity (i.e. going through several onCreate()/onDelete() cycles). Unfortunately, Android’s support for doing that is really poor.

As an easy way to preserve state, I thought that since the class is only loaded once by the class loader, that it would be safe to store temporary data that’s shared between several instances of an activity in a static Bundle field.

However, occasionally, when instance A creates the static bundle and stores data in it, then gets destroyed, and instance B tries to read from it, the static field is suddenly NULL.

Doesn’t that mean that the class had been removed and reloaded by the classloader while the activity was going through a create/destroy cycle? How else could a static field suddenly become NULL when it was referencing an object before?

  • 1 1 Answer
  • 1 View
  • 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-05-12T19:17:26+00:00Added an answer on May 12, 2026 at 7:17 pm

    The first part of this answer is really old — see below for the right way to do it

    You can use the Application object to store application persistent objects. This Android FAQ talks about this problem as well.

    Something like this:

    public class MyApplication extends Application{
        private String thing = null;
    
        public String getThing(){
            return thing;
        }
    
        public void setThing( String thing ){
            this.thing = thing;
        }
    }
    
    public class MyActivity extends Activity {
        private MyApplication app;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            app = ((MyApplication)getApplication());
    
            String thing = app.getThing();
        }
    }
    

    The right way:

    When this answer was first written, the documentation for the Activity lifecycle was not as good as it is now. Reading Saving Activity State section on the Activity document helps us understand how Android wants us to save state. Essentially, there are two circumstances under which your activity starts: (1) as a new activity and (2) because of a configuration change or when it’s recreated after being destroyed due to memory pressure. When your activity starts because it’s a new activity, then saveInstanceState is null. It’s not null otherwise. If it’s null, then your activity should initialize itself from scratch. Fragments are very similar to Activities, and I covered this concept in detail for my AnDevCon-14 slide deck. You can also take a look at the sample code for my AnDevCon-14 presentation for more details.

    Reworking my previous example would look something like the code below. I do change the semantics a bit — in this second version I assume the string thing is specific to the activity within a specific android task, in the previous example it’s ambiguous. If you do want to keep the same data around for multiple android tasks, then using either the Application object or another singleton is still your best bet.

    public class MyActivity extends Activity {
        private static final String THING = "THING";
    
        private String thing;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (savedInstanceState==null) {
                // First time here (since we last backed out at least)
                thing = initializeThing(); // somehow we init it
            } else {
                // Rehydrate this new instance of the Activity
                thing = savedInstanceState.getString(THING);
            }
    
            String thing = app.getThing();
        }
    
        protected void onSaveInstanceState(Bundle outState) {
            outState.putString(THING, thing);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Frequently I have run into a problem when installing gems that provides a problem
One problem that I have frequently run into lately is the problem of my
I frequently run into the problem, that I must extend a compiler generated copy
This is a problem that I run into frequently: Given some horizontal nav links,
I frequently run into the following problem, I was wondering if there is any
I frequently have some code that should be run either on a schedule or
I run into this frequently enough that I thought I'd see what others had
Hey.. I have run into a bit of a problem with my python code..
When using simple DTOs in various scenarios I have frequently run into the same
Looking through some apache logs, I've run into the following pattern several times (URL

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.