![](/img/trans.png)
[英]Passing data from Activity to Activity using Otto is not working
[英]Passing data from Activity to Fragment using Otto
在我的應用程序中,我正在動態地將Fragments添加到主活動視圖中的容器中。 我想知道在添加Fragment時使用Otto時傳遞數據的最佳方法是什么。 目前我正在這樣做,請在示例中我不發布我的CustomObject
在我的主要活動中
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, MY_CUSTOM_FRAGMENT).commit();
BusProvider.getInstance().post(produceCustomString());
在我的片段里面
@Subscribe
public void onCustomStringChanged(String customString) {
}
如果您對已注冊的相同類型也有@Produce
方法,則會自動調用使用@Subscribe
注釋的方法。
告知新數據碎片的最佳方法是在活動中使用@Produce
方法:
@Produce public String produceCustomString() {
return "Hello, World!";
}
然后你的所有片段都有@Subscribe
方法:
@Subscribe public void onCustomStringEvent(String event) {
// ...
}
當您注冊具有此方法的片段時,Otto將調用活動上的@Produce
方法以獲取將傳遞給片段方法的最新值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.