ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 210 forks source link

Only one instance of a fragment in entire of the app #210

Closed hosseinaminii closed 5 years ago

hosseinaminii commented 5 years ago

Hello, I want to show only one instance of SearchFragment, so I wrote this code:

fragNavController.currentStack?.forEachWithIndex { index, it -> if (it.arguments?.get(ARGS_INSTANCE) == 123) { fragNavController.currentStack?.removeAt(index) } } fragNavController.pushFragment(SearchFragment.newInstance(123))

But it doesn't remove the previous instance of the SearchFragment. How can I achieve it? Thanks

ncapdevi commented 5 years ago

Hmm, that's a tough one. You shouldn't be modifying the stack directly, so the above won't work. I believe your use-case would be outside the library's scope unfortunately. Personally that UX seems a bit strange to me. A user navigating through and popping search onto the stack, and navigating to an item, pressing back, you would expect the search fragment again. Not sure what your particular use case, but it doesn't seem to follow android's guideline of a navigation https://developer.android.com/guide/navigation/navigation-principles#navigation_state_is_represented_as_a_stack_of_destinations