Clear static data onDestroy ()

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

+8
android android-activity static
source share
5 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 whatever value you want, but using static variables for other things, constants are usually bad ideas - this is usually a bad class design and can lead to unexpected behavior like the one you observed.

The value of static variables will remain until the class is loaded - it has almost no relation to the activity life cycle ( onCreate , ..., onDestroy )

The first time a class is accessed from code, it will be loaded, and then it will not disappear until there is reason to unload it. During this time, something from your application (technically within the framework of one Process, as a rule, each .apk uses its own), will read the same value from this statics. And if you change it from different places, you change it to other parts that do not know about the change - why is it bad :)

The only reason (I know) that Android will unload the class is because your application is completely deleted from memory - either with the help of a task killer, or when your application is no longer active and the memory becomes low. This is completely out of your control and should not happen while your application is in use. This can happen if, for example, a phone call appears and your application resumes later.

+10
source share

In onDestroy() you can set null values ​​for these static variables ..

EDIT:

static variables are created and initialized when the class containing them is loaded into the virtual machine by the class loader. When the unloaded class or VM has finished, the static variables go "poof". There is usually no need to clean them.

I suppose if you would like you to be able to clear them in the onTerminate () method (in the application) or the onDestroy() method (in action), but this makes little sense and there is no guarantee that either of these two methods will be called.

If for some reason you are paranoid about variables that are not cleared when created (a serious violation of the VM contract), you can clear them in the onCreate() method.

+4
source share

This is not possible; your static variable must be clear when your activity is destroyed. Perhaps your static variable is declared in some other activity, and you are destroying another activity.

 Activity loses almost all the references once get destroyed. 
+1
source share

When your activity star checks your variable. When the action is destroyed, the whole variable is initialized again. Check the value of the variable. This is not possible as soon as the activity destroys the value of the variable. Check this when your variable value is on initialization.

OR Re-initialized variable in the Destroy method. Static variable = "" or Static variable = null.

0
source share

If you reset your onCreate static variables, they are no longer static, and you can just use a non-stationary variable.

In fact, you are most likely to do this better due to the lack of synchronism between the given static variable and the life cycle of the Android application.

0
source share

All Articles