Advantages of Navigation
Some Android developers may have seen itSingle Activity Multiple Fragment
ofApp
, very smooth or very silky to use. When you want to try this development model, you will find thatFragment
Management will be more troublesome. Don't be afraid now,Android SDK
For usNavigation
to implement this development model. I hope this article will inspire you.
Let's talk about use firstNavigation
Advantages:
- It can visualize the navigation of the page, which can help us quickly clarify the relationship between pages;
- pass
destination
andaction
Complete navigation between pages; - It is convenient for us to add page switching animations;
- Type-safe parameter passing between pages;
- pass
NavigationUI
class, unified management of menus, bottom navigation, and drawer menu navigation; - Support deep links
DeepLink
(Deep link).
Navigation dependency settings for project
dependencies { def nav_version = "2.4.2" classpath ":navigation-safe-args-gradle-plugin:$nav_version" }
Navigation dependency settings for module
plugins { id '' }
Java version:
implementation ":navigation-fragment:2.4.1" implementation ":navigation-ui:2.4.1"
Kotlin version:
implementation ":navigation-fragment-ktx:2.4.1" implementation ":navigation-ui-ktx:2.4.1"
Compose version:
implementation ":navigation-compose:2.4.1"
The main factors of Navigation
-
Navigation Graph
, a new type of XML resource file, which containsApp
All pages, and the relationship between pages. -
NavHostFragment
, specialFragment
We can think of it as aFragment
container,Navigation Graph
In-houseFragment
All throughNavHostFragment
Show it. -
NavController
, done in the codeNavigation Graph
Page switching in the work.
Navigation code example
1. Create navigation resource folder in res,navigation
createnav_graph.xml
document.
2. Inactivity_main.xml
, The content details are as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:tools="/tools" xmlns:app="/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <fragment android: android:layout_width="match_parent" android:layout_height="match_parent" android:name="" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true"/> </LinearLayout>
The attribute description is as follows:
-
android:name=""
, tell the system that this is a special oneFragment
。 -
app:defaultNavHost="true"
,expressFragment
The system return key will be automatically processed, and the current return operation will be exited when the user performs the return operation.Fragment
. -
app:navGraph="@navigation/nav_graph"
, Navigation View.
Create two Fragments in nav_graph.xml
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android: app:startDestination="@id/mainFragment"> <fragment android: android:name="" android:label="fragment_main" tools:layout="@layout/fragment_main"> <action android: app:destination="@id/secondFragment" /> </fragment> <fragment android: android:name="" android:label="fragment_second" tools:layout="@layout/fragment_second" /> </navigation>
NavControIIer implements navigation function
We're inMainFragment
Create button in and set the button click event:
<Button>().setOnClickListener { (it).navigate(.action_mainFragment_to_secondFragment) }
This is probably the case. Interested friends can try it.
The above is the detailed content of the Android-Jetpack-Navigation component usage example. For more information about the Android-Jetpack-Navigation component, please follow my other related articles!