Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] - How to use fragment manager? #30

Open
nvhaiwork opened this issue Dec 27, 2023 · 6 comments
Open

[Question] - How to use fragment manager? #30

nvhaiwork opened this issue Dec 27, 2023 · 6 comments

Comments

@nvhaiwork
Copy link

nvhaiwork commented Dec 27, 2023

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

@KaustubhPatange
Copy link
Owner

KaustubhPatange commented Dec 27, 2023

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.

@nvhaiwork
Copy link
Author

Yep, the problem is addOnBackStackChangedListener not getting called

Screenshot 2023-12-27 at 12 53 26

@KaustubhPatange
Copy link
Owner

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

@nvhaiwork
Copy link
Author

@KaustubhPatange i did but still same result

@KaustubhPatange
Copy link
Owner

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.

@nvhaiwork
Copy link
Author

@KaustubhPatange thanks for your support. Let me check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants