0

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?

Arun Swaminathan
  • 351
  • 1
  • 3
  • 10
  • 1
    Sending data between activities in other way as using Intents (startActivity/startActivityForResults/onActivityResults) makes no sens at all... yeah, it is simplification, but think in such way: **There is only one Activity living in your app...** – Selvin Oct 26 '15 at 14:45

2 Answers2

1

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);
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
1

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

mr. Nutscracker
  • 545
  • 1
  • 4
  • 11
  • Can you explain what exactly you mean? – Arun Swaminathan Oct 26 '15 at 15:10
  • Look, first you post an event (`manager.bus.post("Hi");`) and then you start another activity. Your message already went to event bus and is already handled when your second activity is created. So, you should first open another activity and then post your event, or use handler, jst as in the answer above And yes, you should bundle your args when you want to transfer them to another activity instead of posting them with event bus. See [this](http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity) – mr. Nutscracker Oct 27 '15 at 10:20