Subscriber not getting fired when using Otto

筅森魡賤 提交于 2019-12-06 07:44:53

问题


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:

My Bus provider:

public final class BusProvider {

 private static final Bus mInstance = new Bus();

 private BusProvider() {}

 public static Bus getBusProviderInstance() {
     return mInstance;
 }
}

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);
    }

    // ...
}

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;
        }
    }
}

I don't get any errors. But when i click my button from the fragment, the event onButtonChoicePicked doesn't get called.

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?


回答1:


Make sure you are importing "com.squareup.otto.Subscribe" not "com.google.common.eventbus.Subscribe"




回答2:


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.



来源:https://stackoverflow.com/questions/22278135/subscriber-not-getting-fired-when-using-otto

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