Site icon C1CTech

Pass data between destinations using Navigation in Android

&NewLine;<p>This post is about how to pass data between destinations using Android Jetpack Navigation&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;190374&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;PassDataWithNavigation"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine; &NewLine; &NewLine; &NewLine;<h4> <&sol;h4> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Use Safe Args to pass data<&sol;strong><&sol;span><&sol;h3> &NewLine;<p>The Navigation component has a Gradle plugin called <strong><span style&equals;"color&colon; &num;008000&semi;">Safe Args<&sol;span> <&sol;strong>that generates simple object and builder classes that enable type-safe navigation and argument passing between destinations&period;<&sol;p> &NewLine;<p>Safe Args is strongly recommended for navigating and passing data&comma; because it ensures type-safety&period;<&sol;p> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h3> &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<&sol;strong><&sol;span> Activity&comma; provide <span style&equals;"color&colon; &num;008000&semi;"><strong>app<&sol;strong><&sol;span> name&comma; select language to <span style&equals;"color&colon; &num;008000&semi;"><strong>kotlin<&sol;strong><&sol;span> 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; jetpack navigation dependency <&sol;span><&sol;strong><br &sol;> implementation&lpar;"androidx&period;navigation&colon;navigation-fragment-ktx&colon;2&period;4&period;1"&rpar;<br &sol;> implementation&lpar;"androidx&period;navigation&colon;navigation-ui-ktx&colon;2&period;4&period;1"&rpar;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>3&period; To add <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;topic&sol;libraries&sol;architecture&sol;navigation&sol;navigation-pass-data&num;Safe-args">Safe Args<&sol;a><&sol;strong> to your project&comma; requires an additional plugin and one more classpath dependency&colon;<&sol;p> &NewLine;<ul> &NewLine;<li>Open app-level build&period;gradle file and add the below <span style&equals;"color&colon; &num;333333&semi;">plugin<&sol;span> under <strong><span style&equals;"color&colon; &num;008000&semi;">plugins<&sol;span><&sol;strong> section&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>plugins &lbrace;<br &sol;> id 'androidx&period;navigation&period;safeargs&period;kotlin'<br &sol;>&rcub;<&sol;pre> &NewLine;<ul> &NewLine;<li>Open project-level build&period;gradle file&comma; include the following <span style&equals;"color&colon; &num;333333&semi;">classpath&comma; and then click on <&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">Sync Now<&sol;span><&sol;strong>&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>classpath "androidx&period;navigation&colon;navigation-safe-args-gradle-plugin&colon;2&period;4&period;1"<&sol;pre> &NewLine;<h4 id&equals;"add-navhost" role&equals;"presentation" data-text&equals;"Add a NavHost to an activity"><span style&equals;"color&colon; &num;000080&semi;"><strong>Add a NavHostFragment via XML<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>4&period; The below XML file shows a <strong><span style&equals;"color&colon; &num;008000&semi;">NavHostFragment<&sol;span><&sol;strong> as part of an app’s main activity&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;fragment<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;host&lowbar;fragment"<br &sol;> android&colon;name&equals;"androidx&period;navigation&period;fragment&period;NavHostFragment"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> app&colon;defaultNavHost&equals;"true"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"<br &sol;> app&colon;navGraph&equals;"&commat;navigation&sol;nav&lowbar;graph" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<ul> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">android&colon;name<&sol;span>&colon; <&sol;strong>attribute contains the class name of your NavHost implementation&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>app&colon;navGraph&colon;<&sol;strong><&sol;span> defines which Navigation Graph will be associated with the Navigation Host&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>app&colon;defaultNavHost&equals;”true”&colon;<&sol;strong><&sol;span> ensures that the Navigation Host intercepts the system back button when pressed&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p>5&period; Let’s create two empty fragments i&period;e&period;  FragmentA and FragmentB which we will add to nav&lowbar;graph as destinations&period;<&sol;p> &NewLine;<h4 id&equals;"add-navhost" role&equals;"presentation" data-text&equals;"Add a NavHost to an activity"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"devsite-heading" role&equals;"heading" aria-level&equals;"2">Add a Navigation graph<&sol;span><&sol;strong><&sol;span><&sol;h4> &NewLine;<p>6&period; Right-click on the <span style&equals;"color&colon; &num;008000&semi;"><strong>res<&sol;strong><&sol;span> directory and choose <strong><span style&equals;"color&colon; &num;008000&semi;">New -&gt&semi; Android resource file<&sol;span>&period;<&sol;strong> Set the title for the file and choose <span style&equals;"color&colon; &num;008000&semi;"><strong>Navigation<&sol;strong><&sol;span> from the <span style&equals;"color&colon; &num;008000&semi;"><strong>Resource type<&sol;strong><&sol;span> dropdown and then click on <span style&equals;"color&colon; &num;008000&semi;"><strong>ok<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>7&period; The below layout file defines the Navigation Graph&period;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>nav&lowbar;graph&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;navigation xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;nav&lowbar;graph"<br &sol;> app&colon;startDestination&equals;"&commat;id&sol;fragmentA"&gt&semi;<br &sol;><br &sol;> &lt&semi;fragment<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;fragmentA"<br &sol;> android&colon;name&equals;"com&period;c1ctech&period;passdatawithnavigation&period;FragmentA"<br &sol;> android&colon;label&equals;"FragmentA"<br &sol;> tools&colon;layout&equals;"&commat;layout&sol;fragment&lowbar;a"&gt&semi;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;action<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;id&equals;"&commat;&plus;id&sol;action&lowbar;fragmentA&lowbar;to&lowbar;fragmentB"<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> app&colon;destination&equals;"&commat;id&sol;fragmentB" &sol;&gt&semi;<&sol;span><&sol;strong><br &sol;><br &sol;> &lt&semi;&sol;fragment&gt&semi;<br &sol;><br &sol;> &lt&semi;fragment<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;fragmentB"<br &sol;> android&colon;name&equals;"com&period;c1ctech&period;passdatawithnavigation&period;FragmentB"<br &sol;> android&colon;label&equals;"FragmentB"<br &sol;> tools&colon;layout&equals;"&commat;layout&sol;fragment&lowbar;b"&gt&semi;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;argument<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;name&equals;"data"<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;defaultValue&equals;"defaultValue"<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> app&colon;argType&equals;"string" &sol;&gt&semi;<&sol;span><&sol;strong><br &sol;><br &sol;> &lt&semi;&sol;fragment&gt&semi;<br &sol;>&lt&semi;&sol;navigation&gt&semi;<&sol;pre> &NewLine;<ul> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">app&colon;startDestination<&sol;span>&colon; <&sol;strong>defines the starting destination&period; <&sol;li> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">android&colon;name&colon;<&sol;span> <&sol;strong>shows the name of the fragment that is associated with the destination&period;<&sol;li> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">android&colon;label&colon;<&sol;span> <&sol;strong>contains the user-readable name of the destination&period;<&sol;li> &NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">tools&colon;layout&colon;<&sol;span> <&sol;strong>defines the layout of the destination&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Add action and arguments<&sol;strong><&sol;span><&sol;h5> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Action<&sol;strong><&sol;span><&sol;p> &NewLine;<p>In the navigation graph using actions connect both fragments from FragmentA to FragmentB&comma; with a unique ID&period;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Arguments<&sol;strong><&sol;span><&sol;p> &NewLine;<p>FragmentB will receive data from FragmentA&comma; so we have to add an argument to FragmentB&period;<&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;<p>In the navigation graph select FragmentB and from the right side window click the &plus; button in Arguments to add one&period; Provide argument name&comma; type&comma; default value&comma; and then click on <span style&equals;"color&colon; &num;008000&semi;"><strong>Add<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>After adding the argument&comma; rebuild the project&period; Now inside the <strong><span style&equals;"color&colon; &num;008000&semi;">navigation-args<&sol;span><&sol;strong> folder you can see the generated classes using which we will pass data between destinations&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone size-full wp-image-3167" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;04&sol;app&period;png" alt&equals;"" width&equals;"424" height&equals;"361" &sol;><&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Fragment&&num;8217&semi;s layout file<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>8&period; The fragment&lowbar;a&period;xml and fragment&lowbar;b&period;xml define the layout file of Fragments&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">fragment&lowbar;a&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;FrameLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;FragmentA"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> android&colon;gravity&equals;"center"<br &sol;> android&colon;text&equals;"FragmentA"<br &sol;> android&colon;textSize&equals;"30sp"&sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btnGotoFragmentB"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;gravity&equals;"center&lowbar;horizontal&vert;bottom"<br &sol;> android&colon;text&equals;"Goto FragmentB"<br &sol;> android&colon;textAllCaps&equals;"false" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;FrameLayout&gt&semi;<&sol;pre> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">fragment&lowbar;b&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;FrameLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;FragmentB"&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tvFragmentB"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> android&colon;gravity&equals;"center"<br &sol;> android&colon;textSize&equals;"30sp" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;FrameLayout&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Fragment&&num;8217&semi;s kotlin file<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>9&period; The FragmentA&period;kt and FragmentB&period;kt define the two fragments kotlin file&period;<&sol;p> &NewLine;<p>Open <strong><span style&equals;"color&colon; &num;008000&semi;">FragmentA&period;kt<&sol;span><&sol;strong> file and add the below code&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">FragmentA&period;kt <&sol;span><&sol;strong><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;passdatawithnavigation<br &sol;><br &sol;>import android&period;os&period;Bundle<br &sol;>import android&period;view&period;LayoutInflater<br &sol;>import android&period;view&period;View<br &sol;>import android&period;view&period;ViewGroup<br &sol;>import android&period;widget&period;Button<br &sol;>import androidx&period;fragment&period;app&period;Fragment<br &sol;>import androidx&period;navigation&period;findNavController<br &sol;><br &sol;>class FragmentA &colon; Fragment&lpar;&rpar; &lbrace;<br &sol;><br &sol;> override fun onCreateView&lpar;<br &sol;> inflater&colon; LayoutInflater&comma; container&colon; ViewGroup&quest;&comma;<br &sol;> savedInstanceState&colon; Bundle&quest;<br &sol;> &rpar;&colon; View&quest; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Inflate the layout for this fragment<&sol;span><&sol;strong><br &sol;> return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;a&comma; container&comma; false&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onViewCreated&lpar;view&colon; View&comma; savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onViewCreated&lpar;view&comma; savedInstanceState&rpar;<br &sol;> val navController &equals; view&period;findNavController&lpar;&rpar;<br &sol;> val button &equals; view&period;findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;btnGotoFragmentB&rpar;<br &sol;> button&period;setOnClickListener &lbrace;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> val action &equals; FragmentADirections&period;actionFragmentAToFragmentB&lpar;"FragmentB"&rpar;<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> navController&period;navigate&lpar;action&rpar;<&sol;span><&sol;strong><br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<ul> &NewLine;<li>The generated class <span style&equals;"color&colon; &num;008000&semi;"><strong>FragmentADirections<&sol;strong><&sol;span> for FragmentA internally sets the value &&num;8220&semi;FragmentB&&num;8221&semi; to the argument of the name &&num;8220&semi;data&&num;8221&semi; we have created for FragmentB&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p>Open <span style&equals;"color&colon; &num;008000&semi;"><strong>FragmentB&period;kt<&sol;strong><&sol;span> file and add the below code&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">FragmentB&period;kt <&sol;span><&sol;strong><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;passdatawithnavigation<br &sol;><br &sol;>import android&period;os&period;Bundle<br &sol;>import androidx&period;fragment&period;app&period;Fragment<br &sol;>import android&period;view&period;LayoutInflater<br &sol;>import android&period;view&period;View<br &sol;>import android&period;view&period;ViewGroup<br &sol;>import android&period;widget&period;TextView<br &sol;><br &sol;>class FragmentB &colon; Fragment&lpar;&rpar; &lbrace;<br &sol;> private var DATA &equals; ""<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">val args &equals; FragmentBArgs&period;fromBundle&lpar;requireArguments&lpar;&rpar;&rpar;<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> DATA &equals; args&period;data<&sol;span><&sol;strong><br &sol;> &rcub;<br &sol;><br &sol;> override fun onCreateView&lpar;<br &sol;> inflater&colon; LayoutInflater&comma; container&colon; ViewGroup&quest;&comma;<br &sol;> savedInstanceState&colon; Bundle&quest;<br &sol;> &rpar;&colon; View&quest; &lbrace;<br &sol;> &sol;&sol; Inflate the layout for this fragment<br &sol;> return inflater&period;inflate&lpar;R&period;layout&period;fragment&lowbar;b&comma; container&comma; false&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onViewCreated&lpar;view&colon; View&comma; savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onViewCreated&lpar;view&comma; savedInstanceState&rpar;<br &sol;> val textView &equals; view&period;findViewById&lt&semi;TextView&gt&semi;&lpar;R&period;id&period;tvFragmentB&rpar;<br &sol;> textView&period;text &equals; DATA<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<ul> &NewLine;<li>Using the generated class <span style&equals;"color&colon; &num;008000&semi;"><strong>FragmentBArgs <&sol;strong><&sol;span>for FragmentB&comma; we will get the value stored for the data argument&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Writing MainActivity Code<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>10&period; The MainActivity file contains the below code&colon;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;passdatawithnavigation<br &sol;><br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity<br &sol;>import android&period;os&period;Bundle<br &sol;>import androidx&period;navigation&period;NavController<br &sol;>import androidx&period;navigation&period;findNavController<br &sol;>import androidx&period;navigation&period;ui&period;NavigationUI&period;setupActionBarWithNavController<br &sol;><br &sol;>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> lateinit var navController&colon; NavController<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;<br &sol;><br &sol;> navController &equals; this&period;findNavController&lpar;R&period;id&period;nav&lowbar;host&lowbar;fragment&rpar;<br &sol;><br &sol;> setupActionBarWithNavController&lpar;this&comma; navController&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onSupportNavigateUp&lpar;&rpar;&colon; Boolean &lbrace;<br &sol;> navController&period;navigateUp&lpar;&rpar;<br &sol;> return super&period;onSupportNavigateUp&lpar;&rpar;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>In the above code&comma;<&sol;p> &NewLine;<ul> &NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>this&period;findNavController&lpar;&rpar;<&sol;strong><&sol;span> gives us the instance of NavController&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>setupActionBarWithNavController&lpar;&rpar;&colon;<&sol;strong><&sol;span> Sets up the ActionBar returned by <span style&equals;"color&colon; &num;008000&semi;"><strong>AppCompatActivity&period;getSupportActionBar<&sol;strong><&sol;span> for use with a NavController&period;By calling this method&comma; the title in the action bar will automatically be updated when the destination changes&period;<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onSupportNavigateUp&lpar;&rpar;&colon;<&sol;strong> <&sol;span>This method is called whenever the user chooses to navigate Up within your application’s activity hierarchy from the action bar&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3159" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;04&sol;Screenshot&lowbar;2022-04-06-13-22-59-448&lowbar;com&period;c1ctech&period;jetpacknavigation-473x1024&period;png" alt&equals;"" width&equals;"358" height&equals;"775" &sol;>          <img class&equals;"alignnone wp-image-3160" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;04&sol;Screenshot&lowbar;2022-04-06-13-23-07-863&lowbar;com&period;c1ctech&period;jetpacknavigation-473x1024&period;png" alt&equals;"" width&equals;"358" height&equals;"775" &sol;><&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;&NewLine;

Exit mobile version