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?


Thanks.

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
hack...@android.com

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 ( http://developer.android.com/reference/android/app/ 
Activity.html):

"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
management....



public IfWeirdAlWasAProgrammer extends Activity{

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

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

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(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 !=
android.os.Process.myPid()){
processIDChanged = true;
Log.i("WeirdAlWasHere", "I lost on Jeapordy, baby!
Ohhhh!");
}


}
}



Thanks again.



On Jul 14, 1:41pm, Dianne Hackborn <hack...@android.com> 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 <hack...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Porting android on APF27 - Problem

Hi All,


I already read that there was someone who found a problem of boot when
he tried to port android on APF27.

I want to know how he proceeded.

And if he succeeded or not to solve this problem.

Someone can help me ?


Thanks a lot :-)

--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Medical Apps

I am a Physician Assistant looking to buy the new T-Mbile My Touch 3G
phone. I currently have a Palm Treo 680 (which has lots of medical
apps for the Palm!).
I am looking for medical software for the Android OS. Do you know of
any out there? I am having a really hard time finding any! In
particular I am looking for coding apps, calculators, tools, etc. Also
does Epocrates work on android?
Thanks
Tina

--~--~---------~--~----~------------~-------~--~----~

3. How to style my own styleable with a style? (Custom view class with custom attribute)

4. android build error

5. Camera issue that i dont understand (PreviewCallBack/PictureCallback)

6. Quiet mode when calendar Busy?

7. Getting Error "package file was not signed correctly" using Eclipse ADT