I started using OTTO by Square yesterday, so far I had a good start.
Otto works great out of the box when you have your fragments already hosted in FragmentActivity and you just need to communicate between the Fragment host of this FragmentActivity.
When placing, your #onResume () method is already called, and the fragment can be registered on Eventbus:
@Override public void onResume() { super.onResume(); BusProvider.getInstance().register(this); }
My problem:
The fragment embedded in the additional activity that should receive the event through Eventbus is as follows:
public AnotherFragmentHostedInSomeActivity extends Fragment { ..... @Subscribe public void onSomethingHappend(final Event event) { final SomeObject deliveredObject = event.getSomeObject();
But it looks like it's still tricky if you want to trigger another activity where the fragment is placed, such as this code:
public class SomeFragmentSendingDataToAnotherFragment extends Fragment { ... private void sendData() { final Intent intent = new Intent(applicationContext, SomeActivity.class); applicationContext.startActivity(intent); BusProvider.getInstance().post(new Event(someObject));
As you can see, this code is inconvenient. Starting an operation, and then sending data to a fragment located in this work, due to the life cycle. Thus, the activity is created and Fragements. At some point, the onResume Methode method is called so that Fragement can register using @Subscribe. But all this happens after that has already been sent via EventBus. Thus, the interrest fragment is never called EventBus.
Does anyone know how to do this in a smart way?
Additional Information: Yesterday I had a good gameplay with OTTO. The problem occurs only in my project, when I need to send data to another action, which in my case always happens when APP works on a smartphone, and not on a tablet. Before sending all data through Intent and Parcelable. Otto reduced the need to write Parcleable Objects, so I would like to go that route.
thanks for answers