用户没有得到使用奥托时,发射 [英] Subscriber not getting fired when using Otto
问题描述
我想了奥托在Android和我试图从我的片段给活动发回的消息。下面是我的code的基础知识:
I'm trying out Otto on Android and i'm trying to send back a message from my Fragment to the Activity. Here's the basics of my code:
我的车商:
public final class BusProvider {
private static final Bus mInstance = new Bus();
private BusProvider() {}
public static Bus getBusProviderInstance() {
return mInstance;
}
}
我的活动有以下code:
My Activity has the following code:
public class MyActivity
extends BaseActivity {
// ....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BusProvider.getBusProviderInstance().register(this);
// ....
}
@OnClick(R.id.profile_image)
public void onImageClicked() {
// ...
MyFragment fragment = MyFragment.newInstance(choices);
fragment.show(getFragmentManager(), "myChoices");
}
@Subscribe
public void onButtonChoicePicked(MyFragment.ChoicePickedEvent event) {
Toast.makeText(this, "reaching here", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
super.onStop();
BusProvider.getBusProviderInstance().unregister(this);
}
// ...
}
和这些都是code中的重要数据位从我的片段:
and these are the important bits of code from my Fragment:
public class MyFragment
extends BaseDialogFragment {
// ...
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_choices,
container,
false);
setupDialogButtons(inflater, layout);
return layout;
}
private void setupDialogButtons(LayoutInflater inflater, LinearLayout parentView) {
ChoiceButtonViewHolder holder;
holder = new ChoiceButtonViewHolder(inflater, parentView);
holder.populateContent("First Choice", 1);
parentView.addView(holder.mChoiceTextView);
}
class ChoiceButtonViewHolder {
@InjectView(R.id.item_dialog_choice_desc) TextView mChoiceTextView;
private int mPosition;
ChoiceButtonViewHolder(LayoutInflater inflater, ViewGroup container) {
TextView mChoiceTextView = (TextView) inflater.inflate(R.layout.item_dialog_choice, container, false);
ButterKnife.inject(this, mChoiceTextView);
}
public void populateContent(String choiceDesc, int position) {
mChoiceTextView.setText(choiceDesc);
mPosition = position;
}
@OnClick(R.id.item_dialog_choice_desc)
public void onChoiceClicked() {
MyFragment.this.mDialog.dismiss();
BusProvider.getBusProviderInstance().post(new ChoicePickedEvent(1));
}
}
public static class ChoicePickedEvent {
public int mPositionClicked;
ChoicePickedEvent(int position) {
mPositionClicked = position;
}
}
}
我没有得到任何错误。但是,当我点击我的按钮从片段,该事件onButtonChoicePicked不会被调用。
I don't get any errors. But when i click my button from the fragment, the event onButtonChoicePicked doesn't get called.
我是不是做错了什么? 我误解奥托是如何工作的? 它是一个奇怪的组合ButterKnife和奥多,使得它不能正常工作?
Am I doing something wrong? Am i misunderstanding how Otto works? Is it a weird combination of ButterKnife and Otto that makes it not work?
推荐答案
这个例子code工作没有独立于任何的问题。我最初面临这样的问题(如被正确地指出@ powerj1984)原因:有在我的项目,其中被注入(通过匕首)总线是从已被预订的总线实例不同的配置错误更新:P
The example code works without any issues independently. The reason i was facing this problem initially (as was rightly pointed out by @powerj1984): There was a misconfiguration in my project, where the bus that was being injected (via Dagger) was different from the bus instance that was being subscribed to for updates :P.
教训:确保您使用的公交车,在两种情况下,同一个实例
Lesson learnt: make sure the bus you use, is the same instance in both cases.
这篇关于用户没有得到使用奥托时,发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!