6

I have a SearchFragment class which extends a class called BaseFragment in which onResume and onStop are overridden as below:

@Override
public void onResume() {
  checkEventBusRegistration();
    super.onResume();
}
@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}
public void checkEventBusRegistration()
{
    if(!EventBus.getDefault().isRegistered(this))
    {
        EventBus.getDefault().register(this);
    }
}

SearchFragment is a fragment that shows a list of search results. By clicking each item, detail of product is shown on other fragment by below call:

getFragmentManager().beginTransaction().replace(R.id.container, new ProductDetailFragment()).addToBackStack(null).commit();

In addition some other events in my fragment do not work well. My fragment has a listView which does not respond to notifyDataSetChanged().

After returning back from ProductDetailFragment, eventbus subscriber are not triggered and some of events such as notifyDataSetChanged belonging to adapter of my listview do not respond and reflect changes on UI.

Debugging lines of code, after returning back from ProductDetailFragment, when control reaches to SearchFragment.onResume eventbus is still registered and it does not require registration again but generated events do not trigger subscribers.

In case it helps, here thre are the lifecycle events fired by my fragment:

Life cycle events on creating fragment:

onAttach
onCreate
onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu

Life cycle events when leaving this fragment by replacing it:

onPause
onStop
onDestroyView
onDestroyOptionsMenu

Life cycle events when returning back to this fragment:

onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu
VSB
  • 9,825
  • 16
  • 72
  • 145
  • Unregister in ondestroy and check once – Manohar Mar 11 '19 at 09:54
  • @ManoharReddy It did not help. Everything is fine in first run of fragment but it starts to fails after moving to next fragment and coming back.... – VSB Mar 11 '19 at 11:46
  • 1
    Why are you registering in onResume method and unRegistering in onStop method? Shouldn't be easier to use the paired onResume/onPause? – Nicola Gallazzi Mar 11 '19 at 13:30
  • @NicolaGallazzi So what would be the difference? – VSB Mar 13 '19 at 09:58
  • 1
    @VSB you would not need checkEventBusRegistration(), onPause is called right after onResume and you'll not risk leaks on event bus registration. Don't know if this solves your problem but I would give it a try – Nicola Gallazzi Mar 13 '19 at 13:31

2 Answers2

3

You can see that you onStop() is called when the fragment is replaced so the EventBus unregistered:

Life cycle events when leaving this fragment by replacing it:

onPause
onStop
onDestroyView
onDestroyOptionsMenu

And then, when you back to the fragment, your onResume() is called then EventBus is registered:

Life cycle events when returning back to this fragment:

onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu

But when you returning back from ProductDetailFragment your fragment onResume() is not yet called. Hence the subscribe method in the fragment is not called.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • I don't understand this: `But when you returning back from ProductDetailFragment your fragment onResume() is not yet called`? As you can see `onResume` is called when I return back to my fragment – VSB Mar 16 '19 at 06:12
  • When I do return to my fragment, eventbus does not work and that is my problem. – VSB Mar 16 '19 at 06:13
  • what I meant is, your EventBus is only registered when your return to the fragment. The Event is not caught before you called the `onResume()`. Something like this: 1. `onStop() is called` then event bus is unregistered 2. Event is sent but not caught 3. `onResume() is called` then event bus is registered – ישו אוהב אותך Mar 16 '19 at 07:02
  • My problem is not getting triggered when I replaced fragment. The problem is when I return back (by pressing back button) and `onResume` is called and eventBus is registered again but it do not respond to events. – VSB Mar 16 '19 at 11:20
1

If you step through your code with the debugger from where the event is posted, into EventBus.post() to postSingleEvent() to postSingleEventForEventType(), what's the value it gets for subscriptions? If the variable is null or empty and the method returns false, then there is something wrong with the subscription. If it is not, or you never reach this code after your event is posted, then the problem would be somewhere else in your code.

I would also recommend registering and unregistering event subscriptions in matching lifecycle pairs, either register in onStart() and unregister in onStop() or onResume()/onPause().

It would be helpful if you shared more of your code to see where else there could be an issue.

Jeff Lockhart
  • 5,379
  • 4
  • 36
  • 51