aartikov / Alligator

Alligator is Android navigation library that will help to organize your navigation code in clean and testable way.
MIT License
298 stars 17 forks source link

Pop fragments/screens if #5

Closed mjurkus closed 6 years ago

mjurkus commented 6 years ago

What would be the best approach to solve case as described below:

I have fragments/screens [A, B, C, D] in backstack. D is currently active.

Given, that I don't know what fragments are currently in backstack I want to navigate to E, but also remove from backstack D, C, B if they are there. So that end result would be [A, E]

Intention is that user shouldn't see some flow that is no longer relevant to him. And so, that he wouldn't be able to navigate to it via goBack()

I imagine something that. Does it make sense?

fun goToScreenE() {
  [`D`, `C`, `B`].each { screen ->
     if (getCurrentTopScreen()  is screen) {
       navigator.goBack()
     }
  }

  navigator.goForward(E())
}

fun getCurrentTopScreen() {
   ???
}
aartikov commented 6 years ago

There is no such method as getCurrentTopScreen in Navigator. You can only pass commands to a navigator but not query it for something. The reason is that NavigationContext may be not bound yet when you call methods of a navigator.

You can register TransitionListener using NavigationContext to track when a current screen is changed and store this screen somewhere.

I suppose it will be useful to have some commands for more complex manipulations with a screen stack (like that conditional goBack), or even an ability for users to create their own commands. I will think in this direction.

mjurkus commented 6 years ago

When you put it that way - it makes sense.

I'll try to utilize listeners for my purpose.

Custom/pluggable commands sounds really good.