Friday, January 18, 2013

[android-developers] Re: Application.onCreate Method Randomly Called?

>>>>> "Jake" == Jake Colman <colman@ppllc.com> writes:

Jake> Ok. That makes sense but brings it all back full circle.

Jake> It's clear that Application.onCreate is being called multiple
Jake> times for my app. It also seems that my preferences are being
Jake> reset as a result of that happening. But from everything that
Jake> I am reading in this discussion it would seem that should not
Jake> be the case. I guess I have to add some debugging code and
Jake> have my user side-load it to see if I can get some insight into
Jake> the problem.

Jake> Incidentally, this is a fairly straighforward application with
Jake> nothing fancy. It uses an AppWidget class, one Activity, a
Jake> Preference Activity and a Service to do some work on behalf of
Jake> the widget. So any whatever is causing my problem it's not
Jake> because I am being too fancy.

The key that appears to be trashed is used in Service. In the onCreate
method I fetch its value from the shared preferences. The value might
change as the Service does its thing. The key is committed in the
Service's onDestroy method.

The Service is started by my AppWidget every time the app widget needs
to do some work. I do not explicitly stop the Service, relying on
Android to stop it if/when needed (that is not bad practice as I
understand it).

In the above scenario, could the following be happening?

1) Application.onCreate is called
2) Service.onCreate is called
3) Service.onStartCommand is invoked multiple times
4) Application.onCreate is called
5) Service.onCreate is called
6) Service.onStartCommand is invoked multiple times

In other words, would multiple calls to Application.onCreate cause the
Service.onCreate to be called a second time without an intervening
Service.onDestroy? If so, the key would not have been persisted to the
shared preferences and I would have an incorrect value when processing
onStartCommand.

If this is the problem, is it expensive to persist a key as soon as it
is changed? Or should calls to commit be bundled and done as
infrequently as possible?

--
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