Site icon C1CTech

Android Jetpack-Navigation Library Example

<p>The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Navigation Architecture Component<&sol;strong><&sol;span>&comma; released as part of Android Jetpack &comma; aims to simplify the implementation of navigation in your Android app&period; This component and its guidelines offer a new look into how navigation should be implemented&comma; including the suggestion for the use of single-Activity architecture as the preferred architecture for Android Jetpack moving forward&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Principles of Navigation<&sol;strong><&sol;span><&sol;h3>&NewLine;<ul class&equals;"postList">&NewLine;<li id&equals;"b632" class&equals;"graf graf--li graf-after--p">The app should have a fixed starting destination<&sol;li>&NewLine;<li id&equals;"ca85" class&equals;"graf graf--li graf-after--li">A stack is used to represent the navigation state of an app<&sol;li>&NewLine;<li id&equals;"2f26" class&equals;"graf graf--li graf-after--li">The Up button never exits your app<&sol;li>&NewLine;<li id&equals;"331e" class&equals;"graf graf--li graf-after--li">Up and Back are equivalent within your app’s task<&sol;li>&NewLine;<li id&equals;"7683" class&equals;"graf graf--li graf-after--li">Deep linking to a destination or navigating to the same destination should yield the same stack<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 id&equals;"Set-up"><span style&equals;"color&colon; &num;000080&semi;">Set up navigation in a project<&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Note<&sol;span>&colon;<&sol;strong> If you want to use Navigation Architecture Components with Android Studio&comma; you must use <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;studio&sol;preview&sol;">Android Studio 3&period;2 Canary 14 or higher<&sol;a>&period;<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>Add the following Navigation Architecture Component to your app or module&&num;8217&semi;s <code><span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;code> file&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code>dependencies &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Dependencies for Navigation&period;<&sol;span><&sol;strong>&NewLine; def nav&lowbar;version &equals; "1&period;0&period;0-alpha02"&NewLine;&NewLine; implementation "android&period;arch&period;navigation&colon;navigation-fragment&colon;&dollar;nav&lowbar;version" &sol;&sol; use -ktx for Kotlin&NewLine; implementation "android&period;arch&period;navigation&colon;navigation-ui&colon;&dollar;nav&lowbar;version" &sol;&sol; use -ktx for Kotlin&NewLine; &NewLine;&rcub;&NewLine;&NewLine;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>In the Project window&comma; right-click on the <span style&equals;"color&colon; &num;0000ff&semi;"><code>res<&sol;code><&sol;span> directory and select <span style&equals;"color&colon; &num;0000ff&semi;"><strong>New &gt&semi; Android resource file<&sol;strong>&period;<&sol;span> The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>New Resource<&sol;strong><&sol;span> dialog appears&period;<&sol;li>&NewLine;<li>Type a name in the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>File name<&sol;strong><&sol;span> field&comma; such as &&num;8220&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><code>nav&lowbar;graph<&sol;code><&sol;span>&&num;8220&semi;&period;<&sol;li>&NewLine;<li>Select <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Navigation<&sol;strong><&sol;span> from the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Resource type<&sol;strong><&sol;span> drop-down list&period;<&sol;li>&NewLine;<li>Click <span style&equals;"color&colon; &num;0000ff&semi;"><strong>OK<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>Click <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Design<&sol;strong><&sol;span> to see the Navigation Editor&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">The Navigation Editor&&num;8217&semi;s sections are&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<ol class&equals;"callouts">&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">The Destinations list<&sol;span><&sol;strong> &&num;8211&semi; Lists all destinations currently in the Graph Editor&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">The Graph Editor<&sol;span><&sol;strong> &&num;8211&semi; Contains a visual representation of your navigation graph&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">The Attributes Editor<&sol;span><&sol;strong> &&num;8211&semi; Contains attributes associated with destinations and actions in your navigation graph&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>From the Graph Editor&comma; click <strong><span style&equals;"color&colon; &num;0000ff&semi;">New Destination<&sol;span> <&sol;strong> <img class&equals;"inline-icon alignnone" src&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;studio&sol;images&sol;buttons&sol;navigation-add&period;png" alt&equals;"" width&equals;"22" height&equals;"22" &sol;>&period; The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>New Destination<&sol;strong><&sol;span> dialog appears&period;<&sol;li>&NewLine;<li>Click <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Create blank destination<&sol;strong><&sol;span> or click on a fragment or activity&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 id&equals;"Connect-destinations"><span style&equals;"color&colon; &num;000080&semi;">Connect destinations<&sol;span><&sol;h3>&NewLine;<p>Destinations are connected using <strong><span style&equals;"color&colon; &num;0000ff&semi;">actions<&sol;span><&sol;strong>&period; To connect two destinations&colon;<&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;<ul>&NewLine;<li>From the Graph Editor&comma;  over the right side of the destination that you want users to navigate from&period; A circle appears on the destination&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter wp-image-563 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;nav1&period;png" alt&equals;"" width&equals;"305" height&equals;"334" &sol;><&sol;p>&NewLine;<ul>&NewLine;<li>Click and hold&comma; drag your cursor over the destination you want users to navigate to&comma; and release&period; A line is drawn to indicate navigation between the two destinations&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"aligncenter wp-image-564 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;nav&lowbar;img&period;png" alt&equals;"" width&equals;"807" height&equals;"718" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>Click on the arrow to highlight the action&period; The following attributes appear in the Attributes panel&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>The <span style&equals;"color&colon; &num;008000&semi;"><strong>Type<&sol;strong><&sol;span> field contains &OpenCurlyDoubleQuote;Action&period;”<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;008000&semi;">ID<&sol;span><&sol;strong> field contains a system-assigned ID for the action&period;<&sol;li>&NewLine;<li>The <strong><span style&equals;"color&colon; &num;008000&semi;">Destination<&sol;span><&sol;strong> field contains the ID for destination fragment or activity&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 id&equals;"Modify-activity"><span style&equals;"color&colon; &num;000080&semi;">Modify an activity to host navigation<&sol;span><&sol;h3>&NewLine;<p>An activity hosts navigation for an app through the implementation of a <strong><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;navigation&sol;NavHost&period;html"><code>NavHost<&sol;code><&sol;a><&sol;strong> interface which is added to your activity’s layout&period; The <code>NavHost<&sol;code> is an empty view whereupon destinations are swapped in and out as a user navigates through your app&period;<&sol;p>&NewLine;<p>A default implementation of <code>NavHost<&sol;code> is the <span style&equals;"color&colon; &num;000080&semi;"><strong><code>NavHostFragment<&sol;code><&sol;strong><&sol;span> widget which you can add to your layout&period; We can see an example of this below&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;android&period;support&period;constraint&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;container"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;fragment&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;my&lowbar;nav&lowbar;host&lowbar;fragment"&NewLine; android&colon;name&equals;"androidx&period;navigation&period;fragment&period;NavHostFragment"&NewLine; app&colon;defaultNavHost&equals;"true"&NewLine; app&colon;navGraph&equals;"&commat;navigation&sol;nav&lowbar;graph"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;android&period;support&period;constraint&period;ConstraintLayout&gt&semi;&NewLine;&NewLine;<&sol;code><&sol;pre>&NewLine;<h3 id&equals;"Tie-ui"><span style&equals;"color&colon; &num;000080&semi;">Connect destinations to UI widgets<&sol;span><&sol;h3>&NewLine;<pre><code>btn&lowbar;frag1&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar;&NewLine;&lbrace;&NewLine;&commat;Override&NewLine;public void onClick &lpar;View view&rpar;&lbrace;&NewLine;Navigation&period;findNavController&lpar;view&rpar;&period;navigate&lpar;R&period;id&period;action&lowbar;mainFragment&lowbar;to&lowbar;firstFragment&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p>Where <strong><span style&equals;"color&colon; &num;008000&semi;">action&lowbar;mainFragment&lowbar;to&lowbar;firstFragment<&sol;span><&sol;strong> is the action ID from main&lowbar;fragment to first&lowbar;fragment&period;&NewLine;

Exit mobile version