Passing data from Activity to Fragment using Otto

寵の児 提交于 2019-12-04 14:29:49
Jake Wharton

Methods annotated with @Subscribe will automatically be called if you also have a @Produce method for the same type registered.

The best way to inform new fragments of data like this is you have an @Produce method on the activity:

@Produce public String produceCustomString() {
  return "Hello, World!";
}

And then all your fragments which have @Subscribe methods:

@Subscribe public void onCustomStringEvent(String event) {
  // ...
}

When you register a fragment which has this method, Otto will call the @Produce method on the activity to get the latest value which it will pass to the fragment's method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!