How does fragment's lifecycle works inside viewpager? Why onStop is not called on navigation change?
Asked Answered
M

2

1

I'm using ViewPager 2 from AndroidX with 4 instances of the same fragment. My question is pretty straight forward. When I'm navigating to some another fragment(using navigation drawer or even something else). OnStop() , OnDestroy(), OnDettach() of the fragments inside the viewpager does not gets triggered. So why is that? And If I want to remove the listeners I've started already, in one of these methods, how can I do that?

For example, I'm using GreenRobot's EventBus. And I'm registering the EvenBus inside OnStart:

override fun onStart() {
    super.onStart()
    EventBus.getDefault().register(this)
}

And Removing it from OnStop:

override fun onStop() {
    Log.e(TAG, "onStop: ")
    EventBus.getDefault().unregister(this)
    super.onStop()
}

But when I navigate away from the viewpager as I explained above, onStop does not trigger. I even checked it by logging.

So is the fragment lifecycle works differently with viewpager? And if yes, how can I overcome this problem(unregistering EvetBus).

Mahaliamahan answered 13/11, 2019 at 20:23 Comment(4)
Check this out: developer.android.com/reference/android/support/v4/view/…Brio
I know about OffscreenPageLimit, but it is irrelevant here, suppose even if I setOffscreenPageLimit to 1 and navigate away from the viewpager, onStop still won't be called. And won't be able to unregister the listener.Mahaliamahan
Are you using FragmentPagerAdapter or FragmentStatePagerAdapter?Brio
Neither, as I'm using ViewPager 2 from AndroidX, I'm using FragmentStateAdapterMahaliamahan
M
0

Unfortunately, EventBus does not provide great usefulness when it comes to ViewPager and Fragments inside it.

Though I found a solution, using the more traditional approach: Interfaces

It does not directly answer the question Why onStop is not called of fragments inside ViewPager on navigation change?

But it does save you from multiple Event triggers when using EvenBus with ViewPager. With interfaces, As you don't have to explicitly unregister the interface. It does not matter if the onStop is called.

Mahaliamahan answered 14/11, 2019 at 7:15 Comment(8)
Parag, I am facing exactly the same issue with the fragment lifecycle using ViewPager2 and I want to write the code on onDestroy(). Can you help?Anders
@RajatSangrame Sure, can you describe your problem in more detail? What do you want to do on onDestroy?Mahaliamahan
@RajatSangrame You might want to add a new question if it is unique, so that other experienced developer can help you too..Mahaliamahan
I am using ExoPlayer in every fragment. It consumes a lot of memory. I want to release the memory of all the unnecessary fragments.Anders
Did you try releasing the ExoPlayer instance in fragment's onDestroy method?Mahaliamahan
Yes. None of then are executing unless I hit the back button.Anders
Can you add this as your question and post all the code you've tried so far, and share the link, so that I can help you betterMahaliamahan
Let us continue this discussion in chat.Anders
D
-1

You can use setUserVisibleHint to check the fragment visibility.

  override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    super.setUserVisibleHint(isVisibleToUser)

    if (isVisibleToUser) {

       //Fragment is visible

    } else {

       //Fragment is invisible
    }
}

Hope this helps.

Dwaindwaine answered 14/11, 2019 at 1:2 Comment(1)
Nope, I need it for ViewPager 2, this is for Normal ViewPager. Did you read the question?Mahaliamahan

© 2022 - 2024 — McMap. All rights reserved.