tags:

views:

173

answers:

1

I have a widget that shows some data fetched from the Internet. I may have multiple widgets visible on the screen, each displaying some data, partly the same as the other widgets.

From time to time I re-fetch the data from the Internet and update the widget views.

I've modeled this in an MVC style: I have a model which I can invoke a "re-fetch-data" on, and which the views listen on.

My questions:

A) Is it wise to let my model be a singleton?

B) Can I rely on that all widgets are served by the same AppWidgetProvider? Or may some of my widgets get served by a different AppWidgetProvider instance? Or, more generally: Is there any guarantee that there will be only one instance of my AppWidgetProvider?

If there is only one instance of my AppWidgetProvider, then I could use an instance variable here for the model, and pass either my AppWidgetProvider or the model around where needed. Perhaps a better option?

C) In the AppWidgetProviders onUpdate method, can I be sure that the same "context" object is passed as argument each invocation?

+2  A: 

Is it wise to let my model be a singleton?

Considering you can't have static data in an AppWidgetProvider, I would say a singleton is extremely unsafe. You need to use a persistent store, such as a file or database.

Can I rely on that all widgets are served by the same AppWidgetProvider?

It will be the same class. It most definitely will not be the same instance, unless you are doing some really evil stuff, like trying to have an everlasting service.

Or may some of my widgets get served by a different AppWidgetProvider instance?

Yes.

Or, more generally: Is there any guarantee that there will be only one instance of my AppWidgetProvider?

There is a guarantee there will NOT be only one instance of your AppWidgetProvider. "If this BroadcastReceiver was launched through a tag, then the object is no longer alive after returning from this function."

In the AppWidgetProviders onUpdate method, can I be sure that the same "context" object is passed as argument each invocation?

I do not think that is a safe assumption.

CommonsWare
Wow. Great answer. I have some serious refactoring ahead!
aioobe