for quite some time i was blissfully of the opinion that static [instance] variables exist as long as the app runs. however, to my dismay, and much alarm, i feel that it’s not true.
for mere testing, i created a static list of strings, and in my main activity class overrode the onDestroy method to print a message to verify that the app exited. in the onCreate method i simply added a new string, and printed the contents of the list. what i’ve found is that the size of the list keeps on increasing, and all the string values added previously are still present.
i’ve read in places [even here on SO] that the instance variables exist as long as the app does, but i fear it’s not really so.
to be more precise, i came to be aware of this problem while using the Facebook SDK for Android. i’ve seen that the AuthListener instances within the list of listeners in the SessionEvents class just keeps on increasing over time. Hence, whenever the app is launched and the user logs in using FB, the listener methods get triggered as many times as there are instances present in the SessionEvents class.
Has someone observed this before, and is there some cardinal mistake i’m committing in understanding how android works?
what gives?
thanks for reading!
[UPDATE]
I stand corrected by BalusC and rdineiu. I really didn’t mean to create a confusion here about instance and class variables. Alas, in my haste to post my question i’ve committed a mistake i didn’t wish to. I am very well aware of the difference between static and instance variables. I just intended to write class variables, and can’t quite figure out what came over to refer to static variables as instance variables.
However, my question still stands.
@MisterSquonk – no, i’m not confusing here about when my Activity ends and when the app gets destroyed. Here’s what I tried on a sample – I’ve got only ONE Activity which serves as the Main. When I press the back button from this Activity, I’m assuming the Activity gets removed from the stack and the app also gets destroyed. I’ve launched the Task Manager to verify that my app is no longer running.
so – i had posted the same question [unfortunately making the same mistake of terming
staticvariables asinstancevariables] on the Android Developer Google group.I received some really good responses, especially from Kostya. My interactions on that group helped me to grasp the underlying “rules” of the Android platform.
Hope the message thread helps you as well.