Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing Static data onDestroy()

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

like image 811
noob Avatar asked Mar 17 '12 10:03

noob


People also ask

What does it mean for a variable to be static?

What does static mean? When you declare a variable or a method as static, it belongs to the class, rather than a specific instance. This means that only one instance of a static member exists, even if you create multiple objects of the class, or if you don't create any. It will be shared by all objects.

When to use static variables?

use static variables when : The value of the variable is independent of the objects (not unique for each object). E.g. number of students. Show activity on this post. Static variable: When you need something that will be used through out the application and every instance need to know the variable.

What is static variable in java?

In Java, static variables are also called class variables. That is, they belong to a class and not a particular instance. As a result, class initialization will initialize static variables. In contrast, a class's instance will initialize the instance variables (non-static variables).


2 Answers

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.

like image 109
zapl Avatar answered Sep 20 '22 17:09

zapl


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.

like image 38
user370305 Avatar answered Sep 21 '22 17:09

user370305