16

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()?

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184

5 Answers5

15
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.

zapl
  • 63,179
  • 10
  • 123
  • 154
  • Exactly the same I am doing right now, but there are lot's of static variables, which needs to be reset, so I was thinking if there's any better or cleaner way to do this. – 0xC0DED00D Mar 17 '12 at 11:17
  • It works only if you do it manually like in `reset()`. If you have a lot of them you could probably do it with [reflection](http://java.sun.com/developer/technicalArticles/ALT/Reflection/) in a loop over all fields but I wouldn't do that. – zapl Mar 17 '12 at 11:42
5

In onDestroy() you can set null values to those static variables..

EDIT:

Static variables are created and initialized when the class containing them is loaded into the VM by the class loader. When the class is unloaded or the VM ended, the static variables go "poof". There is generally no need to clear them.

I suppose if you wanted to you could clear them in an onTerminate() method (in the application) or an onDestroy() method (in an activity), but there isn't much point in doing so, and there's no guarantee that either of those two methods will be called.

If you're for some reason paranoid about the variables not being cleared when created (a serious violation of the VM's "contract"), you could clear them in an onCreate() method.

user370305
  • 108,599
  • 23
  • 164
  • 151
3

Thats not possible, your static variable must be clear when your activity got destroy. It may be possible that your static variable is declared in some other activity and you destroying other activity.

Activity loses almost all the references once get destroyed.
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • The variables are declared in a separate class, which is not an activity. They gets initialized when the first Activity gets launched. But when I finish the application and checks the values, they retains there value. – 0xC0DED00D Mar 17 '12 at 10:14
  • so for that you have to clear it manually. in onDestroy() of activity staticvariable= ""/null; – Mohammed Azharuddin Shaikh Mar 17 '12 at 10:15
0

If you reset your static variables onCreate they're no longer static and you can just as well use a non static variable.

In fact you're most likely better off doing so due to the lack of synchronicity between specified static variable behavior and Android's app life cycle.

0

When your activity star check your variable.Once activity is destroy all variable is initialized again. Check your variable value. It's impossible once activity is destroy your variable value is same. Check That when your variable value when that initialized.

OR Reinitialized variable on Destroy method. Static variable="" or Static variable=null.

Bhargav Panchal
  • 1,159
  • 1
  • 12
  • 27
  • 1
    Static variable are not initialized per instance wise. They are bind to the class not to the instance. – Praveen Apr 06 '12 at 04:50