I completely agree with Mark:
"Better yet, get rid of the static variables. "
Peter, if possible, it may be 'easier' to change all your non-constant
static variable to member variables of your activity/service by
refactoring.
When you activity/service is destroyed, these variables will go too.
If you still need static access to these variable from other classes,
you can have just one static variable: The currently active instance
of your activity/service. Be sure to set this one to null when
onDestroy is called. In MyActivity:
public static MyActivity ACTIVE_INSTANCE;
onCreate(...) {
ACTIVE_INSTANCE = this;
...
}
onDestroy() {
ACTIVE_INSTANCE = null;
...
}
(Note this will work only if this activity is the only one in your
process. Otherwise, use the onResume and onPause instead).
Other code can now get to your 'static' variables by calling
'ACTIVE_INSTANCE.previouslyStaticVar'
On Jun 3, 8:35 am, Mark Murphy <[email protected]> wrote:
> Peter Carpenter wrote:
> > And this is where my question lies… What happens to the static
> > variables?
>
> They are definitely gone when the process is terminated.
>
> Things get a bit murky if all components are stopped, since outside
> forces (e.g., broadcast intents) might trigger your code again, and it
> might use the same process if that process is still hanging around.
>
> This is one of the many reasons to avoid static variables wherever possible.
>
> > Meaning that a restart of a service does not guarantee a new set
> > of initialised static variables.
>
> Correct.
>
> > Unfortunately the code I’m porting includes lots of static references
> > that assumes an initial clean state and leaves the JVM to clear
> > everything as it’s stopped and started. L
>
> Launch a string of expletives in the general direction of the original
> author. Unless that author is you, of course. ;-)
>
> > Am I forced to pore over this code with a fine tooth comb and
> > ensure that the static variables are initialised correctly (LOTS of
> > work)
>
> Better yet, get rid of the static variables.
>
> > or is there some kind of (dirty) shortcut that I can use that
> > will allow me to effectively unload the application (on activity exit?)
> > forcing the ‘jvm’ to reinitialise the static variables on next activity
> > startup?
>
> You can force-terminate your process via System.exit(). Some people have
> had success with that technique. However, bear in mind this will kill
> off anything that is running in your process, so unless you have
> absolute control over when and how your components run, you might
> clobber something trying to do some work.
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> Warescription: Three Android Books, Plus Updates, $35/Year
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---