I have to solve a theoretical problem and just want to get feedback about the best pattern / practice to solve this in android.

Task: An app could have several activities / fragments showing a list of objects. If one object is present in more than one list at the same time, I want to synchronize changes to this object. So e.g. if I delete the object it should disappear in all lists.

The restricts are that

  • I don't know which lists are currently instantiated
  • Every list has it's own object instance. Data is not shared directly between lists

My solution so far would be to

  • define a Storage class which provides methods for data access
  • create a custom Application class and instantiate the Storage class with the application context (which is needed for e.g. content provider access)
  • the Storage class defines a Listener where each activity can register itself
  • if an activity modifies an object, which it has to do through the (Application) Storage, the Storage class sends an event to each activity / fragment registered so they can adopt the changes.

Does anyone know a better solution for that? And is my solution conform with the android lifecycle?

Thanks a lot for every kind of useful feedback

有帮助吗?

解决方案

If you want to deal in Object then read up on implementing Services and Binding to them. You can provide all your objects with your own API methods and return values. If your data fits into a data cursor model, then you should read up on building your own ContentProvider. It has data observation built in and all the Adapters support it already.

其他提示

Your solution seems pretty good. You could use an EventBus like Otto http://square.github.io/otto/ to send an event to all your activities/fragment when an object is modified.

If you insist on maintaining the separate lists in separate Activity/Fragment instances, then you will be prone to lifecycle problems (you might miss an object modification event while you're paused or something). Otto or other EventBus libraries might help.

However, I think it would be much simpler to host all those lists (and their possibly-shared objects) in a local bindable Service. Each Activity/Fragment can then simply bind/unbind to it in onCreate/onDestroy, thus guaranteeing an up-to-date view of those lists.

See http://developer.android.com/guide/components/bound-services.html

Another similar solution would be to host all those lists in a retained Fragment. Using retained Fragments would be simpler than a Service because you don't have to write all the bind/unbind code. However, retained Fragments might not work as well as a Service if you have multiple Activity components that need access to the same lists of objects.

Yet another hacky solution is to just host all those things in the Application instance itself (effectively a global variable). Then your Activity/Fragment code can just call ((MyApplication) getApplication()).getFooList() to get to those objects.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top