navigator icon indicating copy to clipboard operation
navigator copied to clipboard

[Question] - How to use fragment manager?

Open nvhaiwork opened this issue 1 year ago • 6 comments

if (this::navigate.isInitialized) { childFragmentManager.addOnBackStackChangedListener { Timber.e("addOnBackStackChangedListener") val fragment = navigate.getCurrentFragment() ?: return@addOnBackStackChangedListener if (fragment is BaseFragment<*>) { fragment.onFragmentResume() } } }

Im using navigator(inside fragment) to manage my fragments but when I use above code to addOnBackStackChangedListener nothing happen. I also tried navigate.getFragmentManager() but does not work

nvhaiwork avatar Dec 27 '23 03:12 nvhaiwork

Hi, from what I understand you want to have a callback every time your back stack changes. In that case, you don't need to check if navigator is initialized. Just place attach the listener during init and that would be fine.

class MyFragment: ValueFragment {
   init {
    childFragmentManager. addOnBackStackChangedListener { ... }
  }
}

This will allow you to listen to any changes carried out via childFragmentManager via simpleNavigator. If you want to listen to activity's back stack changes follow the same for activity class.

Also from your code check if addOnBackStackChangedListener is getting called and then proceed with your logic.

KaustubhPatange avatar Dec 27 '23 05:12 KaustubhPatange

Yep, the problem is addOnBackStackChangedListener not getting called

Screenshot 2023-12-27 at 12 53 26

nvhaiwork avatar Dec 27 '23 05:12 nvhaiwork

Try calling navigate.navigate to a different fragment and see if it gets called?

KaustubhPatange avatar Dec 27 '23 05:12 KaustubhPatange

@KaustubhPatange i did but still same result

nvhaiwork avatar Dec 27 '23 06:12 nvhaiwork

This is not a navigator issue but the way how fragments works. addOnBackStackChangedListener is a special API that is only called when you choose addToBackstack option or pass addToBackstack=true in NavOptions when navigating.

If you are not doing it then the listener won't be called.

KaustubhPatange avatar Dec 27 '23 06:12 KaustubhPatange

@KaustubhPatange thanks for your support. Let me check

nvhaiwork avatar Dec 27 '23 06:12 nvhaiwork