Otto event no firing
Asked Answered
C

4

6

I have an activity and it launches a DialogFragment, on completion of an event the DialogFragment posts an event on the Otto Event Bus, this is to fire a method in it's parent activity. I've posted the related code here, the same code is working else where in my app, but here the event is just no firing.

Code in the activity...

 @Subscribe
public void OttoUpdateUI(BudgetUpdateObject budgetUpdateObject)
{
    updateUI();
    Log.d("budget", "Otto updateUI called");
}

@Override
public void onResume() {
    super.onResume();
    BusStand.getInstance().register(BudgetActivityNew.class);
}

@Override
public void onPause() {
    super.onPause();
    BusStand.getInstance().unregister(BudgetActivityNew.class);
}

BusStand class....

public final class BusStand {
private static final Bus BUS = new Bus();

public static Bus getInstance() {
    return BUS;
}

private void BusProvider() {

    }
}

and the firing event...

BusStand.getInstance().post(new BudgetUpdateObject());

I've checked the import in the activity, and I'm not using dagger module, and i'm not using any other event bus. Any help will be much appreciated.

This is the way I launch the DialogFragment from the activity....

AddBudgetDialogFragment addBudgetDialogFragment = new AddBudgetDialogFragment();
addBudgetDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE,0);
addBudgetDialogFragment.show(getSupportFragmentManager(),"DialogFragment");
Continuum answered 23/8, 2015 at 9:14 Comment(4)
How do you launch the Dialog? Could be that you pause the activity on doing this? If the Activity is paused the event is arriving before onResume is calledAlbaalbacete
Code looks fine. Share activity and dialog code.Walcoff
@Igvalle, let me write some log statements and look at what state the activity is when the event is fired...Continuum
@Igvalle, I placed log statements in onResume(), onPause() and the time when the event is fired.... I'm positive that the event is fired only when the activity is registered...Continuum
C
2

Found the answer thanks to these guys.... AndroidAnnotations was overriding @subscribe, so my subsrcibed event was never getting fired, found this by using break points.... Too bad, I moved to EventBus and everything is working fine.... Such a pitty I loved otto so much.....

Continuum answered 25/8, 2015 at 18:8 Comment(1)
Are you sure? AA overrides that methods because actually Otto cannot subscribe to it other way, due to you are using a subclass created by AA, and not the class which are annotated with `@Subscribe'.Whenas
L
5

The issue is that you are not registering the Activity Instance, you are registering the class:

BusStand.getInstance().register(BudgetActivityNew.class);

You should change the code to:

BusStand.getInstance().register(this);

That should do it. :)

Lupus answered 10/9, 2015 at 23:4 Comment(0)
P
4

In my case I imported a wrong library class in my class. Check your imports~

For me, replacing:

import com.google.common.eventbus.Subscribe;

with this:

import com.squareup.otto.Subscribe;

Hope this helps someone.

Pap answered 17/2, 2017 at 4:20 Comment(0)
C
2

Found the answer thanks to these guys.... AndroidAnnotations was overriding @subscribe, so my subsrcibed event was never getting fired, found this by using break points.... Too bad, I moved to EventBus and everything is working fine.... Such a pitty I loved otto so much.....

Continuum answered 25/8, 2015 at 18:8 Comment(1)
Are you sure? AA overrides that methods because actually Otto cannot subscribe to it other way, due to you are using a subclass created by AA, and not the class which are annotated with `@Subscribe'.Whenas
P
-2

You need to register your DialogFragment on the Bus. Post the code for your DialogFragment so I can help you out.

Pillar answered 23/8, 2015 at 10:10 Comment(4)
I thought there is no need to register to post an event.. Am I wrong?Continuum
Your question states "I have an activity and it launches a DialogFragment", and then "the DialogFragment posts an event on the Otto Event Bus". Since the DialogFragment which is a child of Fragment is not registered on the bus, your event doesn't get triggered. Try this out in code yourself, write another sample Fragment class and try launching an event from it even when your Activity is registered.Pillar
After two days of proper research I found out there is no need to register to post an event on the otto event bus, whether you are using a Fragment, DialogFragment, Service or an activity...Continuum
you need to register things on the event bus to receive events. I've written around 3 tutorials about Otto it seems like this is your first time using it.Pillar

© 2022 - 2024 — McMap. All rights reserved.