ncapdevi / FragNav

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

Feature request. Add return to a specific fragment. #226

Closed zakrodionov closed 4 years ago

zakrodionov commented 4 years ago

It would be convenient if there was navigation in a certain fragment. For example, we have a chain a -> b -> c -> d -> e->, and I would like to return,for example, from e to b.

zakrodionov commented 4 years ago
override fun backToFragment(fragment: Class<out Fragment>, refresh: Boolean) {
        val lastCurrentStackIndex = fragNavController.currentStack?.lastIndex ?: 0
        val indexOfFragment =
            fragNavController.currentStack?.indexOfLast { it.javaClass == fragment } ?: -1

        if (indexOfFragment == -1) {
            fragNavController.clearStack()
            fragNavController.pushFragment(fragment.newInstance())
        } else {
            fragNavController.popFragments(lastCurrentStackIndex - indexOfFragment)
        }

        if (refresh) {
            (fragNavController.currentFrag as? BaseFragment)?.refresh()
        }
    }

While using so