Decompose Navigation Example for a BottomNav based UI #178
-
Hi, I'm working on a BottomNav demo to learn how to use Decompose. The sample is currently made for Android (Using AS Arctic Fox 2020.3.1 Beta 5 + Jetpack Compose) but the concepts should translate well for a KMM project. Based on the docs (the routing example) and getting some inspiration from android-dev-challenge-compose-3 I created a demo project located here: DecomposeNavigationSample I have attempted to structure the app like this:
The reason there is a Root and Main component is because I saw it in the dev challenge 3 sample and thought it would be nice to have a mock landing/login page some point in the future:
Questions: (1) How would one navigate from A1 to A2 (same for B and C)? I am not sure how I am suppose to set up navigation from A1 to A2. I have exposed a (2a) Multiple backstacks Ideally I would like it to behave the same way as NavigationAdvancedExample does which is like this: Case 1
Case 2
For the multipe backstacks to work properly, do I need to use multiple routers? (2b) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
@falcon4ever Thanks for good questions! Regarding the question 1. Based on the hierarchy you provided, A2 looks like a permanent child of A1. If you need to switch between A1 and A2, and if you want this navigation to be nested (so there is also a backstack of those nodes in Main), then you need intermediate nodes.
Now you can navigate between A1 and A2 in A, between B1 and B2 in B, and so on. Components A, B, C should have their own |
Beta Was this translation helpful? Give feedback.
-
Regarding questions 2a and 2b. For such a logic you need to do a trick. The idea is to check if the required child's configuration is already in the stack. If it is, then move the configuration to the top the stack (end of the list). If not, then create a new configuration and push it to the stack. You can try the following function, I did not test it but I guess it should work: inline fun <C : Parcelable, reified T : C> Router<C, *>.navigateSingleTop(crossinline config: () -> T) {
navigate { stack ->
val oldConfig: C? = stack.find { it is T }
if (oldConfig != null) {
(stack - oldConfig) + oldConfig
} else {
stack + config()
}
}
} Also because you want the
|
Beta Was this translation helpful? Give feedback.
-
Hi @arkivanov Thank you for your very thorough answers! Looks like I have some work to do here, I'll try to update the sample app and link back when I get things working. I have one more question regarding using Decompose with Jetpack Compose: In the dev challenge samples, it looks like a
and the the RootUi uses this code to render the child(ren) without explicitly saying which ones:
I think that's because the childContent method in the RootComponent is taking care of the routing internally and tying it to a specific UI:
This seem to work for Decompose 0.1.9. When looking at a more recent sample such as the todoapp in the compose-jb repo I see that a different pattern is used (as well in the Decompose 0.2.6 project):
with the UI part:
This does make the Going back to the sample I'm working on, I think that means that in order to display the correct screen for a given tab (A/B/C), I will have to make sure that the A/B/C components will be tied to a composable that looks like this right? (And that this composable will be responsible for routing to the correct child composable).
|
Beta Was this translation helpful? Give feedback.
-
Hi @arkivanov, I've updated the sample and added a readme and your I did leave the It does seem to do the correct thing for |
Beta Was this translation helpful? Give feedback.
@falcon4ever Thanks for good questions!
Regarding the question 1.
Based on the hierarchy you provided, A2 looks like a permanent child of A1. If you need to switch between A1 and A2, and if you want this navigation to be nested (so there is also a backstack of those nodes in Main), then you need intermediate nodes.
Now you can navigate between A1 and A2 in A, between B1 and B2 in B, and so on. Components A, B, C should have their own
Routers
.