<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohamed Shalan</title>
    <description>The latest articles on DEV Community by Mohamed Shalan (@sh3lan93).</description>
    <link>https://dev.to/sh3lan93</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F173591%2Fea8cf711-84d2-4c6e-adcc-2301913e0214.jpeg</url>
      <title>DEV Community: Mohamed Shalan</title>
      <link>https://dev.to/sh3lan93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sh3lan93"/>
    <language>en</language>
    <item>
      <title>Android Navigation Component Issue</title>
      <dc:creator>Mohamed Shalan</dc:creator>
      <pubDate>Tue, 12 Nov 2019 15:51:46 +0000</pubDate>
      <link>https://dev.to/sh3lan93/android-navigation-component-issue-ik3</link>
      <guid>https://dev.to/sh3lan93/android-navigation-component-issue-ik3</guid>
      <description>&lt;p&gt;I had an issue in the application I am currently working on it followed the following scenario; &lt;a href="https://stackoverflow.com/q/58739007/3900842"&gt;Question on stackoverflow&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There is an activity, let's call it PaymentFlowActivity. This activity has only 3 fragments; shipping, review and payment fragment and it doesn't have a NavHostFragment. &lt;/p&gt;

&lt;p&gt;In the Shipping Fragment, I have more than one state, so I decided to add a NavHostFragment for this fragment to handle back stack for those different states. &lt;/p&gt;

&lt;p&gt;so in my fragment layout file, I did the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/root_view"&amp;gt;

        &amp;lt;fragment
            android:id="@+id/shipping_host_nav"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/shipping_nav_graph"
            app:defaultNavHost="true"/&amp;gt;
&amp;lt;androidx.constraintlayout.widget.ConstraintLayout/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when I try to access the nav controller using findNavController() it throws the following exception&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;java.lang.IllegalStateException:xxxxxxxxxxxxx does not have a NavController set&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;when I searched for this exception, I didn't find any useful answers to this use case issue. &lt;/p&gt;

&lt;p&gt;So, I went through the Navigation component code in the SDK and I found the following: when using the findNavController() with a fragment that isn't NavHostFragment or isn't within NavHostFragment this exception will be thrown. &lt;/p&gt;

&lt;p&gt;So we have to find the nav controller by ourselves using the id of the nav host fragment defined in the fragment and the activity as follow&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val navController = Navigation.findNavController(activity, R.id.shipping_host_nav)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will get the navController of your NavHostFragment defined in the fragment layout. &lt;/p&gt;

&lt;p&gt;You can use the navController to navigate to the desired destination&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;navController.navigate(R.id.my_destenation) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make it easier to get the nav controller, I made an extension function for the fragment class that enables accessing the nav controller using only the id. &lt;br&gt;
Here is a snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun Fragment.getFragmentNavController(@IdRes id: Int) = activity?.let {
        return@let Navigation.findNavController(it, id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in your fragment, you can use this method to navigate to the desired destination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getFragmentNavController(R.id.shipping_host_nav)
.navigate(R.id.action_new_address_to_addresses)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>android</category>
      <category>kotlin</category>
      <category>navigationcomponent</category>
    </item>
  </channel>
</rss>
