23.03.2011 16:14, Jake Colman пишет:
If I create an instance of a singleton from within an
activity/service/receiver, am I correct that that same instance is
available to the other components of my application?  If so, then is it
correct to say that all the components of my application share the same
memory space.

Yes, just like in C++ - all Java objects within your application's process are directly accessible from one another.

Incidentally, please correct me if I am using incorrect Android or Java
terminology.

"Scope" seems a little weird here. To me, that means something like :

if (...) {
// scope local variable
int x = ...
}

Maybe it's just me.

If that is the case, how do I know (or should I even try to know) if my
singleton instance has gone out of scope?  Does it get destroyed when
the last component of my application has been killed by Android?  Is
there a way for me to know that?  Do I care?

You don't.

A singleton is entirely managed by your application. The normal scheme is to lazy initialize the singleton when the need arises, and then let it live forever.

In Android, however, this "forever" comes to an end when your application's process is killed. When this happens, the entire contents of its memory space is lost. This is true with any other language, including C++.

The symptom that I am observing with my application is that my singleton
instance has been recreated.  But since my service thinks it has already
initialized the singleton with the necessary data and that data has not
changed.  So the newly initialized singleton is not being reinitialized
to the saved values.

Then you have a bug, potentially in two places (maybe one of them, maybe both):

- How the singleton instance is initialized. The usual pattern is to use lazy-initialization with a global instance reference. Relying on "component A" to create the singleton so that "component B" can use it later can be error-prone.

- How the singleton initializes its state. This can use persistent storage, or initialize (or kick off initialization sequence) when the instance is created.

Is the correct Android paradigm to have my singleton's constructor check
whether there is a saved preference and to reconstruct itself with those

Sure.

values if it exists?  If so, then I can have the service save thosevalues in 
shared preferences since the service certainly knows when it is being stopped.

Under normal circumstances, yes. However, if it's killed by the out of memory process killer, I don't think its onDestroy gets called.

To ensure that your data values are saved, do it as they become available.

--
Kostya Vasilyev -- http://kmansoft.wordpress.com

--
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to