Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Navigating the obstacles of Navigation Compose

Posted on Sep 8 Hello guys 👋👋, welcome back to another article, in our previous articlewediscussed how to inspect network traffic using the Chucker dev extension in android.In this article, I will show you a trick that can save you time when dealing with the nuisances of Navigation ComposeIf you've used navigation compose, you may have noticed how sometimes it canbe a headache because it lacks compile time type safety, hence sometimes whenyou navigate to a given route, your app crashes.This problem has led to the creation of type safe navigation libraries like compose destinations, voyager etc.This can reduce your productivity as may spend time to debug the crash. Basically, to prevent the reoccurrence such crashes, we should abstract our navigation definitions through state hoisting. Don't understand what this means ....read onBasically, state hoisting is practise in Jetpack Compose where we only exposestate and functions that modify the state to the composable.This results tohaving 2 versions of one composable, Stateless and a stateful one.For moreinformation and the benefits of this practise,read this articleby meNormally we navigate in compose by passing the navigation logic to theonClick lambda where its needed as shown belowThe problem with this approach is that when if the NavHost where this navControlleris defined does not have a route called "login", the app will crash. Normally ifyou have one NavHost this won't be the issue since there is only one instanceof the navController(Unless you create a new one in your composable), but whenyour app has a bottom Nav bar that requires its own NavHost, this approach canbecome an issue since now you have multiple navControllersTo solve this issue, we can define all necessary navigation definitions in our NavHost via explicit lambdas or extension functions to the NavControllerclass. This way we know what NavController has a route definition as shown belowThis way we can pass the navigation functions to the composables without explictly having to define the navigation logic each time we need it. If we get the navigationlogic right top level, we won't need to worry about getting it right when actually navigating to the various screens.Once again this won't help much if you have 1 NavHost you will see its benefits ifyou have a bottom App Bar in your application I hope this tricks helps you build better apps using Jetpack compose.Don't forget to clap for this article and leave a comment for any questionsWhile you are at it, you can follow my GitHub profile to see some projects this in concept in practiceTemplates let you quickly answer FAQs or store snippets for re-use.Heyy Peter Chege, you can Improve your post by adding pictures,GIF's and emoji's.😁nice Post ❤️Thank you Next time I will add them :) Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse max-arshinov - Sep 8 Isaac Oresanya - Sep 8 Sparkout Tech - Sep 8 Luca Del Puppo - Sep 8 Once suspended, peter_chege79 will not be able to comment or publish posts until their suspension is removed. Once unsuspended, peter_chege79 will be able to comment and publish posts again. Once unpublished, all posts by peter_chege79 will become hidden and only accessible to themselves. If peter_chege79 is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Peter Chege. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag peter_chege79: peter_chege79 consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging peter_chege79 will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Navigating the obstacles of Navigation Compose

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×