Fragment does not respond to UI updates and eventbus events after resume
Asked Answered
A

2

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
Attract answered 11/3, 2019 at 9:46 Comment(5)
Unregister in ondestroy and check onceBemused
@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....Attract
Why are you registering in onResume method and unRegistering in onStop method? Shouldn't be easier to use the paired onResume/onPause?Bots
@NicolaGallazzi So what would be the difference?Attract
@Attract 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 tryBots
C
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.

Crafton answered 13/3, 2019 at 12:14 Comment(4)
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 fragmentAttract
When I do return to my fragment, eventbus does not work and that is my problem.Attract
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 registeredArturoartus
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.Attract
I
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.

Imperceptive answered 17/3, 2019 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.