It is not surprising at all to me. This is expected behavior:
The process is killed and then there is nothing left. There is nothing to
'come back to'.
Then Android restarts the process and instantiates a new Application object
in that new process. Since the 'now' variable in your sample-code is based
on the system-time, it's no wonder that it will have a different value the
next time it is instantiated.
If you want consistent behavior in singletons where the life-cycle of
objects is not under your control (e.g. activities and such), then make the
accessors of these singleton self-contained. E.g:
public class MyApp extends Application {
private static SomeObject someObject = null;
public static getSomeObject(Context context) {
if (someObject == null) {
someObject = ... ... ... // create/instantiate this object here....
}
return someObject;
}
...
...
}
Then calling the class method MyApp.getSomeObject(ctxt) will *always *return
a proper value, because this method is self contained; it is solely
responsible for creating the singleton (and returning it to the caller).
Of course, if someObject depends on a variable external value (such as the
system's time), then the contents/value of someObject is depending on the
first-time access of this method for this particular process.
}
On Thursday, May 3, 2012 7:24:23 PM UTC-4, blake wrote:
>
> On May 3, 4:15 pm, Streets Of Boston <[email protected]> wrote:
> > You are right that the enum has been initialized a second time.
> > But the second time is a different process (26202 != 26607).
>
> I know! I know! Different process, different class loaders,
> (...oddly, might be the same thread)
>
> > In other words, the first process (26202) did not see this enum being
> > initialized twice. The process was killed and the new process had its
> enum
> > initialized instead.
>
> The point of this whole thread, I believe, is that it is not safe to
> put stuff in to static fields of the application. They will not be
> there when you get back. Among the things that it is not safe to do,
> is to "initialize" the static field with some value and then assume
> that the field will continue to have that value. From the point of
> view of your application/task the field has become uninitialized.
>
> I completely agree that this might not be a bug. It is outright
> documented to happen in an Activity. It is a little surprising, in
> the activity, though...
>
> Blake Meike
> Marakana
>
> The second edition of Programming Android is now on line!
> http://shop.oreilly.com/product/0636920023005.do
>
>
>
--
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