문제

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?

도움이 되었습니까?

해결책 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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top