Arun Swaminathan
Arun Swaminathan

Reputation: 351

Otto Subscribe method not called from Activity to Fragment in Another Activity

This is my first activity where Im making a post call. The bus provider is the default one in the otto sample app.

void openNextActivity()
{
    manager.bus.post("Hi");
    // Intent to my next Activity
}

This is my fragment in another activity where im subscribing for the data. The bus received is the same, however the subscribe method is not being called.

public class ProductListFragment extends BaseFragment  {

     String LOG_TAG = ProductListFragment.class.getCanonicalName();

     public static ProductListFragment newInstance() {
         ProductListFragment fragment = new ProductListFragment();
         return fragment;
     }

     public ProductListFragment() {
        // Required empty public constructor
     }

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          getActivity().invalidateOptionsMenu();
     }

     @Override
     public void onResume() {
         super.onResume();
         BusProvider.getInstance().register(this);
     }

     @Override
     public void onPause() {
       super.onPause();
       BusProvider.getInstance().unregister(this);
    }

    @Subscribe public void onPostRecived(String s) {
       Log.d(LOG_TAG, s);
    }

}

There are no errors on anything being received, however if I put a button onclick on the fragment and post some content from there, the subscribe method is being called. For eg.

@OnClick(R.id.makePostCall) void call() {

     BusProvider.getInstance().post("Hi");
}

I'm getting the appropriate log on this call. Any idea where the code is going wrong?

Upvotes: 0

Views: 581

Answers (2)

tiny sunlight
tiny sunlight

Reputation: 6251

u send msg before intent;the BusProvider id registered after intent; just try:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        BusProvider.getInstance().post("Hi");
    }
},3000);

Upvotes: 1

mr. Nutscracker
mr. Nutscracker

Reputation: 585

it seems you subscribe your second activity's fragment after sending stuff to event bus. Consider changing your logic

Upvotes: 1

Related Questions