I have one main activity with 2 fragments. Both fragments have a ListView. I want to update the list in MainActivity. Is there any way to access fr
You could do the following with Otto event bus:
public class UpdateListEvent {
private int fragmentState;
public UpdateListEvent(int fragmentState) {
this.fragmentState = fragmentState;
}
}
public class MainActivity extends ActionBarActivity {
...
public void updatelist() {
SingletonBus.INSTANCE.getBus().post(new UpdateListEvent(fragmentState));
}
}
public class FragmentA extends Fragment {
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
}
@Override
public void onPause() {
SingletonBus.INSTANCE.getBus().unregister(this);
super.onPause();
}
@Subscribe
public void onUpdateListEvent(UpdateListEvent e) {
if(e.getFragmentState() == 0) { //is this even necessary?
this.adapter.notifyDataSetChanged();
}
}
}
public class FragmentB extends Fragment {
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
}
@Override
public void onPause() {
SingletonBus.INSTANCE.getBus().unregister(this);
super.onPause();
}
@Subscribe
public void onUpdateListEvent(UpdateListEvent e) {
if(e.getFragmentState() != 0) { //is this even necessary?
this.adapter.notifyDataSetChanged();
}
}
}
And a revised version of the Singleton Bus
public enum SingletonBus {
INSTANCE;
private static String TAG = SingletonBus.class.getSimpleName();
private Bus bus;
private volatile boolean paused;
private final Vector