role

Unified management of Fragment, management of the jump between Ftagment and Fragment and Activity jump, pass parameters, etc. You can replace some of the activities in a project with fragments.

Implementation effect

The basic use

Create a navigation

Right-click on the Res File,new->Android Resource File to display the figure

Select “Navigation” for “type” and “OK” for the file name. This will automatically generate a Navigation folder and an XML file

Click on theAdd a created Fragment

Click on theSet a Fragment as the main Fragment, and connect the dot of the main Fragment to another Fragment to indicate the jump connection

Automatically generated code

<? The XML version = "1.0" encoding = "utf-8"? > <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.jetpacklearn.FirstFragment" android:label="fragment_first" 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.jetpacklearn.SecondFragment" android:label="second_fragment" tools:layout="@layout/second_fragment" /> </navigation>Copy the code

App :startDestination=”@id/firstFragment” indicates the start Fragment. The action indicates the Fragment to be queried

Another point is to add a NavHostFragment to the Activity hosting the Fragment to display the Fragment.

activity_main.xml

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView4" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/nav_graph" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

The android: name = “androidx. Navigation. Fragments. NavHostFragment” is a must, App :navGraph=”@navigation/nav_graph” is associated with the previous navigation XML file app:defaultNavHost=”true” will intercept the system’s back button

Fragment Indicates the hop and pass parameter

FirstFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState) val bundleOf = bundleOf("1" to "passed from First ") val STR = arguments? .getString("2") textView2.text = str textView.setOnClickListener { findNavController().navigate(R.id.action_firstFragment_to_secondFragment,bundleOf) } }Copy the code

SecondFragment.kt

override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) val str = arguments? . Get string (" 1 ") textView3. Text = STR val bundleOf = bundleOf (" 2 "to" from SecondFragment passed ") textView4. SetOnClickListener { findNavController().setGraph(R.navigation.nav_graph,bundleOf) } textView7.setOnClickListener { findNavController().navigate(R.id.action_secondFragment_to_secondActivity4) } }Copy the code

Obtaining NavController method fragments. FindNavController () the findNavController () Activity. FindNavController (viewId: Int)

Navigate from the Fragment to findNavController().navigate(). The first parameter is the action ID of the link in the navigation diagram. The second parameter is a wrapped bundle object. The getArgument() method is called when the bundle is obtained from the Fragemnt that receives the parameter.

FindNavController ().setGraph(). The first parameter is the id of the Navigation, and the second parameter is the bundle object. Just call findNavController().navigateUp() if you don’t need to pass arguments to roll back.

If you need to jump from a Fragment to an Activity, create a new Activity in the navigation diagram and connect the Fragment to it, and automatically generate the action. To redirect, call findNavController().navigate() and pass in the Action ID. If you want to jump to an Activity that needs to use fragments again, just start with the dolls from the first step.

If you need to jump from an Activity to an Activity, it is not necessary and Navigation does not support it. Navigation itself is intended to replace the Activity with a Fragment. If you want to do this, you can actually use an Activity to create a host Fragment and then use the Fragment to achieve a jump.

In addition, as far as I understand, the navigation diagram of multiple fragments in each Activty is separate, that is, one Activity corresponds to one navigation diagram. The final destination of each navigation diagram is mostly another Activity.