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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:52:05+00:00 2026-05-31T15:52:05+00:00

In my application there is a class where I’ve declared some static variables. The

  • 0

In my application there is a class where I’ve declared some static variables. The problem is values for all the variables are not resetting when the application is destroyed.
Is there any way to reset the values of all the static variables when the application gets destroyed, except individually resetting them in onDestroy()?

  • 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-05-31T15:52:06+00:00Added an answer on May 31, 2026 at 3:52 pm
    class MySettings {
        // final can't be changed
        public static final String CONSTANT = "ucantchangeme";
    
        // static - all Objects have just these - even different Acitivities etc
        public static String sUserName;
    
        // class instance members - each Object you create with new has its own version
        public String mUserName;
    
        public MySettings() {
            mUserName = "uninitialized";
        }
    
        public static void init() {
            sUserName = "Peter";
        }
    
        public static void reset() {
            sUserName = null;
        }
    }
    
    /* Your Activity */
    class MyActivity extends Actitivy {
        private MySettings mSettings;
        private MySettings mOtherSettings;
    
        onCreate() {
            // init those static parts of MySettings
            MySettings.init();
    
            mSettings = new mSettings();
            mSettings.mUserName = "Peter"
            Log.d("TAG", "Username: " + mSettings.mUserName);
            // prints Peter
    
            // this could also be another Activity
            mOtherSettings = new MySettings();
            Log.d("TAG", "Username: " + mOtherSettings.mUserName);
            // prints "uninitialized"
    
            MySettings.sUserName = "Jim";
            Log.d("TAG", "Username: " + MySettings.sUserName);
            // prints "Jim"
    
            mSettings.sUserName = "Joe";
            Log.d("TAG", "Username: " + MySettings.sUserName);
            Log.d("TAG", "Username: " + mSettings.sUserName);
            Log.d("TAG", "Username: " + mOtherSettings.sUserName);
            // all print "Joe" now
        }
    
        onDestroy() {
            // clear MySettings
            MySettings.reset();
        }
    }
    

    You can reset static variables to null or any value you want but using static variables for other things then constants is usually a bad idea – it’s usually bad class design and can lead to unexpected behaviour like the one you observed.

    The value of static variables will persist as long as the class is loaded – it has almost nothing to do with Activity lifecycle (onCreate, …, onDestroy)

    The first time you access a class from code it will get loaded and then it won’t go away until there is a reason to unload it. During that time anything from within your app (technically within the same Process – usually each .apk uses its own) will read the same value from those statics. And if you change it from different places you change it for other parts that don’t know of the change – that’s why it’s bad 🙂

    The only reason (I know of) that Android will unload a class is that your app gets completely removed from memory – either via a task-killer or when your app is no longer active and memory gets low. That is completely out of your control and should not happen while your app is used. It could happen if e.g. a phone call comes in and your app resumes later.

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

Sidebar

Related Questions

how to check wifi connection throught the entire application.is there any events or class
Is there any class or how to draw windows in console application using pseudo
Is there any class, library or some piece of code which will help me
Is there a class/example application for a message-only window that is in C++ Win32
i have a question. There is application class in my program. It is inherited
I'm working on a Grails application. There is a domain class participant - within
We all know that most applications out there assume class names to follow the
In my web application there is a process that queries data from all over
In my Rails application there is a model that has some has_one associations (this
In my application there are some configuration data, such as ipaddress, portno, title and

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.