(I apologize for the fact that in my first post it was not so clear)
Here is the situation: I have data that needs to be updated from the Internet. Let me call it Model .
What I want to do: Basically, it looks like an MVC model, where Model also stored in local (private) storage. Model and its associated methods are applied. There are several Activity that display and manipulate various aspects:
- The user navigates through different
Activity that display Model from different points of view. I currently have a ListActivity for all items, and an Activity for one item for details Model sometimes needs updating. Of course, this is being done on a different topic. An update can be triggered by multiple Activity .- There are several (time-consuming) common tasks that can be run from different
Activity - My application loads and saves
Model to private storage when it starts and stops
My problem: I'm not sure where to put Model and its related tasks. In addition, I do not know what mechanism to use to notify Activity . I am currently coming up with two approaches:
- Use
Service and send broadcasts. Saving to disk is done in Service#onDestroyed() , so I want to minimize this by binding it to Activity . At this point, I also do not know how to deliver updated information: whether to provide getter in Binder or to include this in a broadcast message. - Customize your
Application object so that update methods and getters are available worldwide. Then I upgrade from Activity using AsyncTask . If there are other Activity that are behind the current Activity , they will be updated in onResume() when the user goes back.
Reasons I do not use a class with static methods:
- I need to save and save
Model to disk. - Some of the methods need Context to display toasts, notifications, caching, etc.
In addition, I do not put these functions in the Activity , because there are several actions that control the same part of the persistent data.
The following are pseudo codes illustrating what I mean:
Use of service:
/** Service maintaining state and performing background tasks */ class MyService extends Service { Model mModel; Binder mBinder; onCreate() { super.onCreate(); mBinder = new Binder(); // load mModel from disk, or do default initialization } onDestroy() { super.onDestroy(); // save mModel to disk } onBind() { return mBinder; } class Binder { refresh() { new AsyncTask() { doInBackground() { // update mModel from Internet } onPostExecute() { sendBroadcasts(new Intent("my.package.REFRESHED")); } }.execute(); } getState() { return mModel.getState(); } } } /** Activity displaying result */ class MyActivity extends ListActivity { MyService.Binder mBinder; onCreate() { super.onCreate(); // register mReceiver // bind service } onDestroy() { super.onDestroy(); // unbind service // unregister mReceiver } /** Invokes time-consuming update */ refresh() { // binding is asynchronous, and user may trigger refreshing too early if (mBinder != null) { mBinder.refresh(); } } BroadcastReceiver mReceiver = new BroadcastReceiver() { onReceive(Intent intent) { if ("my.package.REFRESHED".equals(intent.getAction()) && mBinder != null) { updateViews(mBinder.getState()); } } }; }
Make globally accessible functionality in a custom application object
class MyApplication extends Application { Model mModel; onCreate() { super.onCreate();
The weaknesses that I can come up with for the Service approach is complexity, since the binding is asynchronous. And it is very likely that I should repeat some code, because I have both ListActivity and Activity
For the Application approach, the documentation says that you should not rely on the called onTerminate() .
I know that I am very embarrassed. What is the usual way to solve this problem?
Many thanks.
android design model-view-controller service
Phil
source share