Activity lifecycle and static variables ....

by Richard Schilling » Wed, 15 Jul 2009 03:52:58 GMT

Sponsored Links
 I just ran into a situation where it looks like a static variable
reference is persisted across activity sessions.  I didn't expect that
because I thought that when an activity exits, it's de-referenced and
garbage collected.

I am wondering if anyone can shed some (more) light on when the VM
eliminates object references for Activities and Services and in
particular when static variables get reset to default values?


Richard Schilling
Root Wireless


Activity lifecycle and static variables ....

by Dianne Hackborn » Wed, 15 Jul 2009 04:24:27 GMT

 Statics are statics -- they are global to the process.  So their value will
last for the lifetime of the process, which is usually much longer than an
individual activity instance.

On Tue, Jul 14, 2009 at 12:52 PM, Richard Schilling <

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


Sponsored Links

Activity lifecycle and static variables ....

by Richard Schilling » Wed, 15 Jul 2009 04:38:54 GMT

 Thanks Dianne.  Very helpful indeed.

For process lifetime, I don't think you're referring to the "entire
lifetime" of an Activity as defined on the API reference page for
Activity ( 

"The entire lifetime of an activity happens between the first call to
onCreate(Bundle) through to a single final call to onDestroy()".

Rather, you're referring to a broader lifetime of the process that
underlies the Activity.... correct?  This would be the lifetime
associated with android.os.Process I assume....

Richard Schilling
Root Wireless


Activity lifecycle and static variables ....

by Richard Schilling » Wed, 15 Jul 2009 05:59:48 GMT

 es.  I had read that, and I understand that the VM tries to stratify
processes depending on their function and keep my process alive as
long as possible. What I'm really interested in is detecting when the
the VM (re)-instantiated and garbage collects my Activity class, and
how that relates to the process identifier...

So, with this code, would procewssIDChanged ever be true? I know it's
a mundane question, but it tells me a lot about Android process

public IfWeirdAlWasAProgrammer extends Activity{

private static boolean newInstance = true;
private static int currentProcessId = -1;

private boolean processIDChanged = false; // non-static on purpose

public void onCreate(Bundle savedInstanceState) {

if (newInstance){
Log.i("WeirdAlWasHere", "Like a surgeon, cuttin' for the
very first tiiiiime.");
newInstance = false;
currentProcessId = android.os.Process.myPid();

if (!newInstance && (currentProcessId !=
processIDChanged = true;
Log.i("WeirdAlWasHere", "I lost on Jeapordy, baby!


Thanks again.

On Jul 14, 1:41pm, Dianne Hackborn <> wrote:

Activity lifecycle and static variables ....

by Richard Schilling » Wed, 15 Jul 2009 09:10:15 GMT

 hat affirms a lot for me.  Thanks!

Richard Schilling
Root Wireless

On Jul 14, 4:35pm, Dianne Hackborn <> wrote:

Other Threads

1. Making my app a library, resource problems

Okay, in order to make it easier (I hope) to have both a Licensed full
version, and a Demo version of my app, I've converted my App project in
Eclipse into an Android Library project.
I created a new Library project, the copied all the source and resources
over, set the manifest values, etc...
I then created a new App project to be the licensed version.  It uses the
library project.

What I'm seeing is that the App project builds a for both the App and
the Library.  BUT it builds the one for the library incorrectly.  It's
leaving out several resources from my portrait layout.
When the Library project is build, it's has all the resources.

It's also interesting that the App project builds an for the App, and
an for the Library project - and they are IDENTICAL.
This also indicates that you have to be really careful about not having
conflicting resource names between and App and a Library it uses.


2. uninstall TTS data from Android >= 1.6 ?

How do you uninstall voice data from a phone that has it installed?

I need to test code that handles cases of it being absent.



3. zoom and drag code registering but not actually working...

4. yahoo local search xml reply in android

5. Creating a neon scroll animation

6. How to implement this with location.distanceTo()?

7. Nexian Journey