I have an activity (main
) with three fragments (first, second and third). I included the 3 fragments in my activity (activity_main.xml
) by using <include layout="@layout/content_main"/>
.
The content_main.xml
is using FragmentContainerView
with id = nav_host_fragment
. And this is my nav_graph.xml
:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="com.example.makegroups.FirstFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
</fragment>
<fragment
android:id="@+id/SecondFragment"
android:name="com.example.makegroups.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second">
<action
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
<action
android:id="@+id/action_SecondFragment_to_ThirdFragment"
app:destination="@id/ThirdFragment" />
</fragment>
<fragment
android:id="@+id/ThirdFragment"
android:name="com.example.makegroups.ThirdFragment"
android:label="@string/third_fragment_label"
tools:layout="@layout/fragment_third">
<action
android:id="@+id/action_ThirdFragment_to_FirstFragment"
app:destination="@id/FirstFragment" />
</fragment>
</navigation>
I have a floatingactionbutton
in my activity (first fragment
starts first) and when I click on it, I open the third fragment
.
On the third fragment
I have a button (next) to navigate to the first fragment
, and when I click on it, I am back to first fragment
using:
Fragment frg = new FirstFragment();
FragmentManager fm = requireActivity().getSupportFragmentManager();
Now (while I am in the first fragment
), I click on the button next
(another button to navigate to the second fragment
), then the app crashes. I found this error:
java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{c9572fa V.E...... ........ 0,0-1440,2112} does not have a NavController set
Why I am getting this error? -I tried these suggestions here, without success.
I am using Java.
EDIT: Read the last comment with @Zain to know why I got the error.
Bu using Navigation Architecture components, The NavController is the responsible for fragment transaction and managing the back stack instead of the Support FragmentManager
.
So, instead of making tranditional framgnet transactions with FragmentManager
You can move from ThridFragment to the first one by:
Navigation.findNavController(requireView()).navigate(R.id.action_ThirdFragment_to_FirstFragment);
Where action_ThirdFragment_to_FirstFragment
is the id of the action you defined in the navigation graph to move from ThridFragment
to FirstFragment
UPDATE:
As discussed from comments, besides replacing FragmentManager
by NavController
in all actions; there is another issue:
Missing of action_FirstFragment_to_ThirdFragment
action from the navigation graph.