Android - is onDestroy supposed to destroy the activity, its variables and free up memory
Asked Answered
C

1

9

I have a bug in my code that made me think I don't fully understand the Android Lifecycle. Yes, I have read all the docs and looked at the diagrams, but they seem to talk only about when to save data, when the activity may loose focus or get killed. However, my question is if I don't need to save state, what happens to the variables & their stored values? I expected them to be destroyed to, but a bug in my code seems to indicate otherwise.

In my case here is what happened. I have an activity that launches a custom view (no xml, I just draw bitmaps on the screen in my custom view). The only variable I currently have in my activity is just a variable for my view: GameView gameView;

Now in my view, I declare several bitmaps, simple int and float variable to deal with drawing and on touch events and I have one array of objects that contain small bitmaps, coordinates of each objects and a few other things. One of the variables in my class for this object, is a static variable that represents the current count of how many objects their are. I did it this way, so the instantiation of the objects causes it to track how man objects their are, instead of tracking this outside the object's class.

I expected the static variable to stay the same value across all objects, but I also expected this variable to be destroyed along with all the other variables and objects of that Activity's view once onDestroyed was called for that Activity. However, that doesn't seem to happen. When this Activity is launched again, this static variable still contains its previous value from its last run - even though onDestroyed was called.

Now my question is NOT how to fix this (I can write code differently to fix this bug), but I would like to understand why this happens with this static variable, since it isn't global to the whole application, it only exists inside that Activity's view? Also, this makes me wonder about the rest of the variables in that view - are they destroyed and their memory released or at least their values no longer available the next time the activity is called or do I need to do this myself - even though I didn't need to save any of this state data?

Thanks for any insight into this.

Chauffer answered 16/12, 2010 at 14:50 Comment(0)
I
9

onDestroy is an instance method and any memory it releases (or allows the garbage collector to release) will be of the corresponding instance. Activities are not singleton; there can be more than one instance of an Activity.

Static variables are class variables and are accesible to all instances of that class. They are initialized when the class is loaded, not when each instance of the class is created.

Please see Understanding Instance and Class members for more info. An excerpt:

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

Implied answered 16/12, 2010 at 14:56 Comment(3)
Thanks,I knew that static variables is the same memory location across all objects, but I didn't know that "class variables can also be manipulated without creating an instance of the class.". That would explain why when the objects are destroyed why this value hangs around. BTW, when I say onDestroy, I haven't created it or any lifecycle method other than onCreate - yet. That is why when I hit the back button, expected all data associated with that instance of the Activity to have been deleted. But now I understand why this one variable stuck around. Thanks.Chauffer
Therein lies the rub. A static variable is not data associated to any instance of the class.Implied
If my Activity holds a reference of another class's object(Simple class not Activity) and is an Instance Member, so will the method onDestroy() clear it for me, or I need to clear it explicitly (set to null) ?Liam

© 2022 - 2024 — McMap. All rights reserved.