Im working with RecyclerView, SyncAdapter and greenrobot eventbus
When my SyncAdapter finished syincing i post a message into the message bus:
EventBus.getDefault().post(new EventMessagesRefreshed());
In my target class i do the following:
@Subscribe
public void onEvent(EventMessagesRefreshed event) {
this.init();
}
And in my init() i create the adapter for the recyclerview and set it:
public void init() {
if(this.listRowParent != null) {
this.adapter = new FragmentMessagesListAdapter(this.getActivity(), SingletonMessages.getInstance().getMessages());
this.listRowParent.setAdapter(this.adapter);
}
}
// listRowParent is my RecyclerView!
The fragment which receives the event is inside a of a tab view. So there are multiple tabs and sometimes ofcourse the SyncAdapter posts the EventMessagesRefreshed into the message bus when im not in the correct target tab but since it is registered it tries to call init() and to create the adapter and set it to the RecyclerView. If that happens i get the following error:
Could not dispatch event: class EventMessagesRefreshed to subscribing class class FragmentMessagesList
java.lang.IllegalStateException: Observer android.support.v7.widget.RecyclerView$RecyclerViewDataObserver@2c3421a7 was not registered.
at android.database.Observable.unregisterObserver(Observable.java:69)
at android.support.v7.widget.RecyclerView$Adapter.unregisterAdapterDataObserver(RecyclerView.java:5688)
at android.support.v7.widget.RecyclerView.setAdapterInternal(RecyclerView.java:873)
at android.support.v7.widget.RecyclerView.setAdapter(RecyclerView.java:857)
So i need to init() my adapter and RecyclerView only when the RecyclerViewDataObserver is registered.
How can i do that?