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");
Giridhar Karnik
  • 2,213
  • 4
  • 27
  • 47
  • 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 called – lgvalle Aug 23 '15 at 09:21
  • Code looks fine. Share activity and dialog code. – Noman Rafique Aug 23 '15 at 09:22
  • @Igvalle, let me write some log statements and look at what state the activity is when the event is fired... – Giridhar Karnik Aug 23 '15 at 09:38
  • 1
    @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... – Giridhar Karnik Aug 23 '15 at 09:44

4 Answers4

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. :)

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.

Jiyeh
  • 5,187
  • 2
  • 30
  • 31
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.....

Giridhar Karnik
  • 2,213
  • 4
  • 27
  • 47
  • 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'. – WonderCsabo Sep 03 '15 at 10:52
-2

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

AndyRoid
  • 5,062
  • 8
  • 38
  • 73
  • I thought there is no need to register to post an event.. Am I wrong? – Giridhar Karnik Aug 23 '15 at 10:14
  • 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. – AndyRoid Aug 23 '15 at 10:16
  • 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... – Giridhar Karnik Aug 25 '15 at 17:45
  • 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. – AndyRoid Aug 25 '15 at 19:04