Site icon C1CTech

Android Jetpack Compose Navigation Example

&NewLine;<p>This post is about how to navigate between different composable using the Navigation component with Jetpack compose&period;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Navigation<&sol;strong><&sol;span> refers to the interactions that allow users to navigate across&comma; into&comma; and back out from the different pieces of content within your app&period; Android Jetpack&&num;8217&semi;s Navigation component helps you implement navigation&comma; from simple button clicks to more complex patterns&comma; such as app bars and the navigation drawer&period;<&sol;p> &NewLine;<p>The Navigation component provides support for Jetpack Compose applications&period; You can navigate between composables while taking advantage of the Navigation component’s infrastructure and features&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-a89b3969 wp-block-buttons-is-layout-flex"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;140466&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;JetpackComposeNavigationExp"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<h3> <&sol;h3> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>1 &period; Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Android Project<&sol;strong><&sol;span>&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Compose Activity<&sol;strong><&sol;span>&comma; provide <span style&equals;"color&colon; &num;008000&semi;"><strong>app<&sol;strong><&sol;span> name and then finally click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>2 &period; Open app-level build&period;gradle file and under the <span style&equals;"color&colon; &num;008000&semi;"><strong>dependencies<&sol;strong><&sol;span> section add the below libraries and then sync the project<strong>&colon;<&sol;strong><&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;navigation with compose<&sol;span><&sol;strong><br &sol;> implementation&lpar;"androidx&period;navigation&colon;navigation-compose&colon;2&period;4&period;2"&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>Get the latest navigation-compose dependency from <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;jetpack&sol;compose&sol;navigation&num;setup">here<&sol;a><&sol;strong>&period;<&sol;p> &NewLine;<p>Basically&comma; there are two main parts of a Jetpack Compose Navigation&colon;<&sol;p> &NewLine;<ul> &NewLine;<li>NavController<&sol;li> &NewLine;<li>NavHost<&sol;li> &NewLine;<&sol;ul> &NewLine;<h4> <&sol;h4> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>NavController<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>NavController<&sol;strong><&sol;span> is the central API for the Navigation component&period;<&sol;p> &NewLine;<p>It is stateful and keeps track of the back stack of composables that make up the screens in your app and the state of each screen&period; So to navigate to any destination&comma; we need to use the NavController&period;<&sol;p> &NewLine;<p>To create a NavController&comma; you can use <strong><span style&equals;"color&colon; &num;008000&semi;">rememberNavController&lpar;&rpar;<&sol;span><&sol;strong> method in your composable&colon;<&sol;p> &NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating navController instance<&sol;span><&sol;strong><br &sol;>val navController &equals; rememberNavController&lpar;&rpar;<&sol;pre> &NewLine;<h4> <&sol;h4> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">NavHost<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>The NavHost links the NavController with a navigation graph that specifies the composable destinations that you should be able to navigate between&period;<&sol;p> &NewLine;<p>As you navigate between composables&comma; the content of the NavHost is automatically <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;jetpack&sol;compose&sol;mental-model&num;recomposition"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>recomposed<&sol;strong><&sol;span><&sol;a>&period;<&sol;p> &NewLine;<p>Each NavController must be associated with a single NavHost composable&period;<&sol;p> &NewLine;<p>Each composable destination in your navigation graph is associated with a route &lpar; String that defines the path to your composable&rpar;&period;<&sol;p> &NewLine;<p>Creating the NavHost requires the NavController and the route of the starting destination of your graph&period;<&sol;p> &NewLine;<p>You can add to your navigation structure by using the <span style&equals;"color&colon; &num;008000&semi;"><strong>composable&lpar;&rpar;<&sol;strong><&sol;span> method&period; This method requires that you provide a route and the composable that should be linked to the destination&colon;<&sol;p> &NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;creating a NavHost<&sol;strong><&sol;span><br &sol;>NavHost&lpar;<br &sol;> navController &equals; navController&comma;<br &sol;> startDestination &equals; "first&lowbar;screen"<br &sol;>&rpar; &lbrace;<br &sol;> composable&lpar;"first&lowbar;screen"&rpar; &lbrace;<br &sol;> FirstScreen&lpar;navController &equals; navController&rpar;<br &sol;> &rcub;<br &sol;> composable&lpar;"second&lowbar;screen"&rpar; &lbrace;<br &sol;> SecondScreen&lpar;navController &equals; navController&rpar;<br &sol;> &rcub;<br &sol;> composable&lpar;"third&lowbar;screen"&rpar; &lbrace;<br &sol;> ThirdScreen&lpar;navController &equals; navController&rpar;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<h4> <&sol;h4> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Navigate to a composable<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>To navigate to a composable destination in the navigation graph&comma; you must use the <span style&equals;"color&colon; &num;008000&semi;"><strong>navigate&lpar;&rpar;<&sol;strong><&sol;span> method&period;<&sol;p> &NewLine;<p>The navigate&lpar;&rpar; takes a single String parameter that represents the destination’s route&period;<&sol;p> &NewLine;<p>To navigate from a composable within the navigation graph&comma; call navigate&lpar;&rpar;&colon;<&sol;p> &NewLine;<pre>Button&lpar;<br &sol;> onClick &equals; &lbrace;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;navigate to third screen<&sol;span><&sol;strong><br &sol;> navController&period;navigate&lpar;"third&lowbar;screen"&rpar;<br &sol;> &rcub;<br &sol;>&rpar;<&sol;pre> &NewLine;<h4> <&sol;h4> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>The Complete MainActivity code<&sol;strong><&sol;span><&sol;h4> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre>class MainActivity &colon; ComponentActivity&lpar;&rpar; &lbrace;<br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContent &lbrace;<br &sol;> JetpackComposeNavigationExpTheme &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; A surface container using the 'background' color from the theme<&sol;span><&sol;strong><br &sol;> Surface&lpar;<br &sol;> modifier &equals; Modifier&period;fillMaxSize&lpar;&rpar;&comma;<br &sol;> color &equals; MaterialTheme&period;colors&period;background<br &sol;> &rpar; &lbrace;<br &sol;> MainScreen&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun MainScreen&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating navController instance<&sol;span><&sol;strong><br &sol;> val navController &equals; rememberNavController&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating a NavHost<&sol;span><&sol;strong><br &sol;> NavHost&lpar;<br &sol;> navController &equals; navController&comma;<br &sol;> startDestination &equals; "first&lowbar;screen"<br &sol;> &rpar; &lbrace;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;You can add to your navigation structure by using the composable&lpar;&rpar; method&period;<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;This method requires that you provide a route and the composable that should be linked to the destination&period;<&sol;span><&sol;strong><br &sol;> composable&lpar;"first&lowbar;screen"&rpar; &lbrace;<br &sol;> FirstScreen&lpar;navController &equals; navController&rpar;<br &sol;> &rcub;<br &sol;> composable&lpar;"second&lowbar;screen"&rpar; &lbrace;<br &sol;> SecondScreen&lpar;navController &equals; navController&rpar;<br &sol;> &rcub;<br &sol;> composable&lpar;"third&lowbar;screen"&rpar; &lbrace;<br &sol;> ThirdScreen&lpar;navController &equals; navController&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun FirstScreen&lpar;navController&colon; NavController&rpar; &lbrace;<br &sol;><br &sol;> Column&lpar;<br &sol;> modifier &equals; Modifier&period;fillMaxSize&lpar;&rpar;&comma;<br &sol;> verticalArrangement &equals; Arrangement&period;Center&comma;<br &sol;> horizontalAlignment &equals; Alignment&period;CenterHorizontally<br &sol;> &rpar; &lbrace;<br &sol;> Text&lpar;<br &sol;> text &equals; "First Screen"&comma;<br &sol;> fontSize &equals; 40&period;sp&comma;<br &sol;> color &equals; Color&period;Blue&comma;<br &sol;> fontWeight &equals; FontWeight&period;Bold<br &sol;> &rpar;<br &sol;><br &sol;> Button&lpar;<br &sol;> shape &equals; RoundedCornerShape&lpar;8&period;dp&rpar;&comma;<br &sol;> modifier &equals; Modifier&period;padding&lpar;all &equals; Dp&lpar;8F&rpar;&rpar;&comma;<br &sol;> onClick &equals; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; navigate to second screen<&sol;span><&sol;strong><br &sol;> navController&period;navigate&lpar;"second&lowbar;screen"&rpar;<br &sol;> &rcub;&comma;<br &sol;> colors &equals; ButtonDefaults&period;buttonColors&lpar;<br &sol;> backgroundColor &equals; Color&period;Blue&comma;<br &sol;> contentColor &equals; Color&period;White<br &sol;> &rpar;<br &sol;> &rpar;<br &sol;> &lbrace;<br &sol;> Text&lpar;text &equals; "Go to Second Screen"&comma; fontSize &equals; 25&period;sp&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun SecondScreen&lpar;navController&colon; NavController&rpar; &lbrace;<br &sol;> Column&lpar;<br &sol;> modifier &equals; Modifier&period;fillMaxSize&lpar;&rpar;&comma;<br &sol;> verticalArrangement &equals; Arrangement&period;Center&comma;<br &sol;> horizontalAlignment &equals; Alignment&period;CenterHorizontally<br &sol;> &rpar; &lbrace;<br &sol;> Text&lpar;<br &sol;> text &equals; "Second Screen"&comma;<br &sol;> fontSize &equals; 40&period;sp&comma;<br &sol;> color &equals; Color&period;Magenta&comma;<br &sol;> fontWeight &equals; FontWeight&period;Bold<br &sol;> &rpar;<br &sol;><br &sol;> Button&lpar;<br &sol;> shape &equals; RoundedCornerShape&lpar;8&period;dp&rpar;&comma;<br &sol;> modifier &equals; Modifier&period;padding&lpar;all &equals; Dp&lpar;8F&rpar;&rpar;&comma;<br &sol;> onClick &equals; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;navigate to third screen<&sol;span><&sol;strong><br &sol;> navController&period;navigate&lpar;"third&lowbar;screen"&rpar;<br &sol;> &rcub;&comma;<br &sol;> colors &equals; ButtonDefaults&period;buttonColors&lpar;<br &sol;> backgroundColor &equals; Color&period;Magenta&comma;<br &sol;> contentColor &equals; Color&period;White<br &sol;> &rpar;<br &sol;> &rpar;<br &sol;> &lbrace;<br &sol;> Text&lpar;text &equals; "Go to Third Screen"&comma; fontSize &equals; 25&period;sp&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<br &sol;><br &sol;>&commat;Composable<br &sol;>fun ThirdScreen&lpar;navController&colon; NavController&rpar; &lbrace;<br &sol;> Column&lpar;<br &sol;> modifier &equals; Modifier&period;fillMaxSize&lpar;&rpar;&comma;<br &sol;> verticalArrangement &equals; Arrangement&period;Center&comma;<br &sol;> horizontalAlignment &equals; Alignment&period;CenterHorizontally<br &sol;> &rpar; &lbrace;<br &sol;> Text&lpar;<br &sol;> text &equals; "Third Screen"&comma;<br &sol;> fontSize &equals; 40&period;sp&comma;<br &sol;> color &equals; Color&period;Red&comma;<br &sol;> fontWeight &equals; FontWeight&period;Bold<br &sol;> &rpar;<br &sol;><br &sol;> Button&lpar;<br &sol;> shape &equals; RoundedCornerShape&lpar;8&period;dp&rpar;&comma;<br &sol;> modifier &equals; Modifier&period;padding&lpar;all &equals; Dp&lpar;8F&rpar;&rpar;&comma;<br &sol;> onClick &equals; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; navigate to first screen<&sol;span><&sol;strong><br &sol;> navController&period;navigate&lpar;"first&lowbar;screen"&rpar;<br &sol;> &rcub;&comma;<br &sol;> colors &equals; ButtonDefaults&period;buttonColors&lpar;<br &sol;> backgroundColor &equals; Color&period;Red&comma;<br &sol;> contentColor &equals; Color&period;White<br &sol;> &rpar;<br &sol;> &rpar;<br &sol;> &lbrace;<br &sol;> Text&lpar;text &equals; "Go to First Screen"&comma; fontSize &equals; 25&period;sp&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3255" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;05&sol;Screenshot&lowbar;2022-05-28-21-16-45-802&lowbar;com&period;c1ctech&period;jetpackcomposenavigationexp1-473x1024&period;png" alt&equals;"" width&equals;"247" height&equals;"535" &sol;>    <img class&equals;"alignnone wp-image-3256" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;05&sol;Screenshot&lowbar;2022-05-28-21-16-53-456&lowbar;com&period;c1ctech&period;jetpackcomposenavigationexp1-473x1024&period;png" alt&equals;"" width&equals;"246" height&equals;"532" &sol;>    <img class&equals;"alignnone wp-image-3257" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;05&sol;Screenshot&lowbar;2022-05-28-21-16-59-695&lowbar;com&period;c1ctech&period;jetpackcomposenavigationexp1-473x1024&period;png" alt&equals;"" width&equals;"245" height&equals;"530" &sol;><&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;&NewLine;

Exit mobile version