Custom Adapter Using Bound Service - When To Unbind?
Solution 1:
Implement ActivityLifecycleCallbacks
in your CustomAdapter and then from your activity call
mAdapter.setActivity(this);
Adapter class
publicclassCustomAdapterextendsArrayAdapter<String> implementsActivityLifecycleCallbacks{
Activity mActivity;
publicvoidsetActivity(Activity activity) {
/* or you could remove setactivity and do below operation in Constructor */
mActivity = activity;
mActivity.getApplication().registerActivityLifecycleCallbacks(this);
}
@OverridepublicvoidonActivityDestroyed(Activity activity) {
/*unregister so that you do not get callbacks after activity is destroyed*/if(activity instanceofMainActivity)
mActivity.getApplication().unregisterActivityLifecycleCallbacks(this);
/*unregister your activity or in any other callbacks*/
}
/* skipped other dummy functions*/
}
Solution 2:
Connection is private to adapter
is wrong. Adapter is Context-less and so a service should not be private to the adapter.
Your adapter will never be able to bind to the service without a Context. Inherently, you have a function that provides a Context for your adapter to bind to the service. Therefore, the bound connection is not private to your Adapter: It is the Context passed that allowed its creation.
The real problem is that AFAIK there is no way to listen to the context. Meaning, say that you held the context instance inside your adapter: There is no way to allow the adapter to listen to this context and somehow know that the Activity is done. This leads you to accept the not-elegant-enough-for-you solution: Your activity should notify the adapter which is using its context! Otherwise, you will end up with a more hacked-up solution.
Please notice that the whole debate in the comments deals with your "service is private to the adapter" thingy. In reality, the elegant way to do this would be to have a ServiceProvider interface that provides the connection and this would be implemented by your activity. You adapter should be initialized with a ServiceProvider passed. Some functions would be getConnection
and isRunning
. In this way, your Activity will take care of the connection while the Adapter uses it. (Synchronization)
Solution 3:
I would use a Service to insert fresh data into a SQLite table or to refresh a ContentProvider, then use a SimpleCursorAdapter (android.support.v4.widget.SimpleCursorAdapter which is backward compatible) and the LoaderManager, SQLiteCursorLoader/CursorLoader, Loader pattern to show the data on a List or ListFragment.
The SQLiteCursorLoader is not given by the Android SDK, but Mark Murphy have one on in GitHub, so you could take a look at it if you want to persist the fresh data in a local SQLite DB.
Since the Service can be independent from the underlying data in SQLite and the Adapter for the list you can unbind it when is finished inserting new data or refreshing the provider so you can procede with the recommended patterns to unbind Services (See this question: Android: How to safely unbind a service) and take a look at http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle.
I really hope this gives you a hint. Good luck!
Post a Comment for "Custom Adapter Using Bound Service - When To Unbind?"