I use the Notifications interface to update snippets every time the data changes.
public interface Notifications { void register(ID id, Listener listener); void unregister(ID id, Listener listener); <T> void post(ID id, T value); interface Listener<T> { void onEvent(ID id, T value); } enum ID { CustomersUpdated, ProductsUpdated } }
As for Android Lifecycle , what is the best way to register and unregister for notifications?

Here are a few scenarios:
Scenario 1:
public class ProductsListFragment extends BaseFragment implements Notifications.Listener { @Override public void onStart() { mAdapter.notifyDataChanged(); register(Notifications.ID.ProductsUpdated, this) super.onStart(); } @Override public void onStop() { unregister(Notifications.ID.ProductsUpdated, this) super.onStop(); } @Override public void onEvent(Notifications.ID id, Object value) { mAdapter.notifyDataChanged(); }
Scenario 2:
public class ProductsListFragment extends BaseFragment implements Notifications.Listener { @Override public void onResume() { mAdapter.notifyDataChanged(); register(Notifications.ID.ProductsUpdated, this) super.onResume(); } @Override public void onPause() { unregister(Notifications.ID.ProductsUpdated, this) super.onPause(); } @Override public void onEvent(Notifications.ID id, Object value) { mAdapter.notifyDataChanged(); }
Please explain why you suggest to use this or that implementation .. or another!
source share