https://kotlinlang.org logo
h

Halil Ozercan

07/13/2020, 9:11 PM
Hello everyone, is there currently a suggested way of handling navigation in compose? Especially stacked navigation where the bottom view is expected to remain as it is in case user navigates back(kind of like using
add
in fragment transactions).
1
z

Zach Klippenstein (he/him) [MOD]

07/13/2020, 9:39 PM
h

Halil Ozercan

07/13/2020, 10:12 PM
I've tried using this library but as I said it doesn't seem to support stacked navigation. Maybe that's even discouraged, idk. I also checked out the sample project which had a back press supporting navigation. However, it also recomposes the pages when user returns back to them. Biggest give away by just using is the fact that it doesn't preserve scroll position for lists.
@andylamax I strongly believe it will come out with an official router or an official extension to use jetpack navigation. Although I admit the latter is unlikely.
z

Zsolt

07/13/2020, 10:18 PM
@Halil Ozercan 1. yes it recomposes (imagine if it wouldn’t), but it also allows you to save / restore state 2. not sure I understood your stacked example right, but if you use nested Routers and not just a flat one for navigation, it shouldn’t be a problem
h

Halil Ozercan

07/13/2020, 10:23 PM
From your example, I understood how to save/restore the state which is awesome. Also, using nested routing makes sense for most use cases but imagine:
Using bottom bar for navigation where there are 3 different screens: Home, Profile, Settings.
Default start screen is Home. User scrolls down some content, pagination occurs multiple times. State gets kind of large for save/restore in memory.
User goes to Profile and then Settings.
Then user presses back two times to go back to Home.
Now, this doesn't feel like a case where nested routing would work(does it?). Secondly, recomposing the Home page is expensive at this point. It would be better to leave Home page under other pages. How can I achieve this behavior?
z

Zsolt

07/13/2020, 10:46 PM
“State gets kind of large for save/restore in memory” - what exactly goes in your state? I guess you can keep it lean by only having descriptive data (position, ids) to use as cues for restoring more expensive data from other sources instead of storing big objects. “It would be better to leave Home page under other pages” - Maybe someone else can chime in to confirm / deny if this is possible with Compose, i.e. to compose composables without rendering them to the ui.
👍 1
i

Ian Lake

07/13/2020, 10:49 PM
FWIW, integration of the Navigation component with Compose is already in the works. That includes saving the state of composables on the back stack
👍 5
z

Zach Klippenstein (he/him) [MOD]

07/13/2020, 10:52 PM
You can also try this, which doesn’t implement a router but just gives you transition animations and, more importantly, saving state of previous screens on the backstack: https://github.com/zach-klippenstein/compose-backstack (it’s a couple compose versions behind i think)
👍 3
m

Madhava

07/13/2020, 11:45 PM
Oh thats awesome, i'm using compose-router and was wondering about transitions! 🙂
3 Views