OTTO and fragments in other activities

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

+7
source share
3 answers

By the time the second action begins, your original action will disappear. As others have pointed out, if you want to transfer data from Activity to Activity, using Intents is the best route.

If you really need an event bus, you need an object that remains alive even after the first action. On Android, this is related to the application context or using Dagger or Guice, @Singleton.

This singleton is where you use the Otto @Produce annotation. When the second activity subscribes to the bus, it will receive any data from this @Produce method.

+16
source

You can check out Jake Wharton's answer on the Otto github issue . You can use @Producer.

when you make HTTP requests and store the response in a cache somewhere (say, a list of employees). Most people will initiate a request, and their activity will act directly when the answer is triggered by some callback. Problems arise here when this callback occurs after onPause and before onResume, as happens when the device is rotated. Instead, you can post an event when an HTTP call returns to the bus so that someone listens to it and also sends it to the cache. Then you may have a manufacturer who talks to the cache to get the results of the HTTP call if any new subscribers are interested in the answer. Thus, when you pause your activity (and unregister) and receive a call back as soon as you resume (and re-register), your producer will be called and the action will be notified.

+1
source

Have you tried using the @Produce annotation? That's right, when your fragment in the second step is registered on the bus, any @Subscribe method that has the corresponding @Produce method will be launched.

0
source

All Articles