Wednesday, June 1, 2011

[android-developers] Re: Android LifeCycle and Singleton Instances

So as long as I always call my getInstance() class I can be certain that
I will get a pointer to the already constructed object or that the
static instance variable had been reset to NULL triggering new
construction?


>>>>> "DH" == Dianne Hackborn <hackbod@android.com> writes:

DH> If your process is killed, the next time you run a fresh process
DH> must be created and re-initialized. There is no way to get a
DH> "stale" pointer across this. If the pointer was stale, you'd
DH> probably have a native crash because it would be completely
DH> unrelated to the current process you are running.

DH> You can verify the behavior yourself just by using adb shell to
DH> kill your process.

DH> On Wed, Jun 1, 2011 at 5:39 PM, Jake Colman <colman@ppllc.com> wrote:

>>
>> I was pretty sure that I understood the Android lifecycle and how
>> applications/processes might be start/stopped/removed and how that
>> effects whether variables remain initialized. I am, however, seeing
>> some funky behavior in my app such that I am afraid I have missed
>> something.
>>
>> I use a singleton pattern for some of my objects. That is, the
>> constructor is private and you call a static getInstance() method to
>> construct the object. The static instance variable is, of course,
>> initialized to NULL which is the trigger for getInstance to know whether
>> the object needs to be constructed.
>>
>> I have learned that any code that calls getInstance() cannot assume that
>> the object it has constructed continues to live in memory since my
>> application might have been killed by Android. So throughout my code I
>> always call getInstance() to ensure that I have a valid object. Am I
>> correct in assuming that had I been killed that getInstance() will
>> create a new object? In other words, will my static instance variable
>> have been reset to NULL or might my factory method give me back a stale
>> pointer?
>>
>> ...Jake
>>
>>
>> --
>> Jake Colman -- Android Tinkerer
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscribe@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>>

DH> --
DH> Dianne Hackborn
DH> Android framework engineer
DH> hackbod@android.com

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

DH> --
DH> You received this message because you are subscribed to the Google
DH> Groups "Android Developers" group.
DH> To post to this group, send email to android-developers@googlegroups.com
DH> To unsubscribe from this group, send email to
DH> android-developers+unsubscribe@googlegroups.com
DH> For more options, visit this group at
DH> http://groups.google.com/group/android-developers?hl=en

--
Jake Colman -- Android Tinkerer

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment