OTTO and Fragments in other Activitys
Asked Answered
S

3

7

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 an FragmentActivity and you just need communicate between Fragment hosted by that FragmentActivity.

When hosted already, your #onResume() Methode gets called and the Fragment can register itself on the Eventbus:

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

My Problem:

The Fragment which is embedded in an extra Activity which should recieve the Event via the Eventbus looks like this:

public AnotherFragmentHostedInSomeActivity extends Fragment
{
         .....

    @Subscribe
    public void onSomethingHappend(final Event event)
    {

          final SomeObject deliveredObject = event.getSomeObject();

But it looks like things are still complicated when you want to call another activity hosting a Fragment like this Code there:

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 might already see, this code is dodgy. Starting a Activity an then sending Data to the Fragment hosted by that Activity cant work because of the lifecycle. So the Activity get created and the Fragements also. At some time the onResume Methode gets called so the Fragement can register isself using @Subscribe. But this all happens after the event already got posted via the EventBus. So the Fragment of interrest never gets called by the EventBus.

Anyone knows how to do this in a smart way ?

Some extra Infos there: I had a nice playaround yesterday with OTTO. The Problem only aprears in my Project when i need to send Data to another Activity which is in my case happening always when the APP runs on an Smartphone, not an Tablet. Before i send all data via Intent and Parcelable. Otto would reduce the need writing Parcleable Objects so i would like to go this way.

Thanks for Answers

Stgermain answered 7/11, 2012 at 10:40 Comment(0)
V
16

By the time the second Activity starts, your original Activity is gone. As others have pointed out, if you want to pass data from Activity to Activity, using Intents is probably the best route.

If you really want an event bus, you need an object that stays alive even after the first activity goes away. In Android, this is something tied to the Application context or if using Dagger or Guice, a @Singleton.

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

Vital answered 8/11, 2012 at 17:3 Comment(1)
Thanks Eric for clearing that. I had a differnt impression of that the Bus can do. The Lifecycle simply does not play the game there. So i still need to know if my Fragment is in some kind of Tablet or Phone 'mode' to fire the Event.Stgermain
I
1

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

when you are making HTTP requests and keeping the response cached somewhere (say, a list of employees). Most people would kick off a request and have their activity directly act when a response comes in on some callback. Problems arise here when that callback happens after onPause and before onResume, as happens when a device is being rotated. What you can instead do, is publish an event when the HTTP call returns on the bus so that anyone listening receives it and also send it to the cache. Then you can have a producer which talks to the cache to produce the results of the HTTP call if any new subscribers are interested in the response. This way, when you pause your activity (and unregister) and have a the call return, once you resume (and re-register) your producer will get called and the activity will be notified.

Intuitive answered 4/12, 2015 at 11:40 Comment(0)
M
0

Have you tried using the @Produce annotation? Right when your Fragment in the second Activity registers itself on the bus, any @Subscribe method that has a corresponding @Produce method will be run.

Marylouisemaryly answered 7/11, 2012 at 13:45 Comment(4)
I tried this. I added also some DeadObject Subscriber to get all undelivered Objects. I still see that its get called. I digged tru the OTTO Code. For me the Problem looks like the Activity gets ramped up Async to OTTO, at some point the onResume gets called, but thats way to late in that scenario. I have to delay the post call to OTTO to get this working i think, which is dodgy aswel. IntentTool.showForecastActivity(getSherlockActivity().getApplicationContext()); BusProvider.getInstance().post(new Event(object));Stgermain
To me it sounds like you're using the Event Bus incorrectly. If you need to post an event as an Activity is starting, pass the data through the Intent.Marylouisemaryly
Well, i did this before. If you want to keep things simple i want to use the Bus if possible. As i else have two methods which are responsible in handling the incoming data.Stgermain
If you want to respond to an event right as the Fragment is created, use the @Produces method thenMarylouisemaryly

© 2022 - 2024 — McMap. All rights reserved.