Preamble
The Navigation component, which is part of the AndroidX package, is introduced since Android SDK 28. This component includes the structure for your application, especially for navigation between fragments.
Principles of Navigation: The application should have a fixed starting point The Navigation Up function should never exit your application
Configuration build.gradle
1 2 3 4 5 6 7 8 9 10 11 | buildscript { ... repositories { google() } dependencies { ... classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:2.1.0' } } |
Implement in app
1 2 3 | implementation 'android.arch.navigation:navigation-fragment:2.1.0' implementation 'android.arch.navigation:navigation-ui:2.1.0' |
** And apply **
1 2 | apply plugin: 'androidx.navigation.safeargs' |
Build UI and navigation in the app with Navigation
Creating an ACtivity with a Fragment Host tag is the master to lead to other fragments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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"> <fragment android:id="@+id/my_nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/navigation_graph" /> </androidx.constraintlayout.widget.ConstraintLayout> |
app: navGraph: Defines the navigation file to own all the links contained in Host Fragment
app: defaultNavHost = “true”. The navigation system prevents the Back system from leaving the app when the Back button is pressed
** Destination: Destination until performing a move action ** ** The first step we create the xml Navigation file with Resource type is Navigation ** We can define an Action by dragging and dropping as follows Or you can create Action tags like the following file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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/navigation_graph" app:startDestination="@id/firstFragment"> <fragment android:id="@+id/firstFragment" android:name="com.FirstFragment" tools:layout="@layout/navigation_first_fragment" > <action android:id="@+id/action_first_Fragment_to_second_Fragment" app:destination="@id/secondFragment" app:enterAnim="@anim/nav_default_enter_anim" /> </fragment> <fragment android:id="@+id/secondFragment" android:name="com.SecondFragment" tools:layout="@layout/navigation_second_fragment" /> </navigation> |
** Next to rebuild app ** After that
There are 2 ways to redirect to another fragment
1 2 3 4 5 6 7 8 9 | button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FindNavController().navigate(FirstFragmentDirections.actionfirstFragmentToSecondFragment() } }); |
Pass arguments every time you change direction. For example
1 2 3 4 5 6 7 8 9 10 11 12 | <fragment android:id="@+id/firstFragment" android:name="com.FirstFragment" android:label="navigation_first_fragment" tools:layout="@layout/navigation_first_fragment" > <argument android:name="test_string" android:defaultValue="hello world" app:argType="string" /> </fragment> |
then in Fragment 1
1 2 3 4 5 6 7 8 9 10 11 | button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { findNavController().navigate( firstFragmentDirections.actionFirstFragmentToTwoFragment( "Chuỗi String" ) ) } }); |
Received in Fragment 2
1 2 3 4 | private val args: FirstFragmentArgs by navArgs() //Sau đó ở nơi lấy ra args.test_string |
** To pop the current Fragment we use. findNavController (). popBackStack () ** When Redirecting fragments. The navigation allows us to capture events that change direction with the destination. For example
1 2 3 4 5 6 7 8 9 10 | private lateinit var navController: NavController navController = Navigation.findNavController(this, R.id.nav_host_container) navController.addOnDestinationChangedListener { _, destination, _ -> when (destination.id) { R.id.firstFragment->{ } //Todo more } |
OK, got it Above is the knowledge about switching between fragments using Navigation. Thanks for your interest and follow up. Bye !!