Jetpack Navigation Component in One Video by Mitch
Get started with the Navigation component by Google Developers
Android Jetpack: Introducing Navigation Component by google
- Simplifed setup for common navigation patterns
- Handles backstack
- Automates fragmnet transactions
- Type safe arrguments passing
- Handles transition animations
- Simplified deep linking
-
Navigation graph: An XML resource that contains all navigation-related information in one centralized location. This includes all of the individual content areas within your app, called destinations, as well as the possible paths that a user can take through your app.
-
NavHost: An empty container that displays destinations from your navigation graph. The Navigation component contains a default
NavHost
implementation, NavHostFragment, that displays fragment destinations. -
NavController: An object that manages app navigation within a
NavHost
. TheNavController
orchestrates the swapping of destination content in the NavHost as users move throughout your app
List by mitch
- Creating a project with Kotlin and androidX
- Navigation Component dependencies
- Creating a navigation graph
- NavHostFragment (Navigation Host Fragment)
- Adding fragments to the navigation graph
- NavController and navigating between fragments
- Fragment transition animations using the navigation graph
- Backstack management with PopUpTo and PopUpToInclusive attributes
- Sending data as Arguments through a Bundle to a Fragment
list by Me
😎
-
Add dependencies in build.gradle app
def nav_version = "2.1.0" implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
-
Create Navigation Graph
res > new android resource file > (file name) (Res type Navigation)
-
Create fragment - use fragment blank (class and layout)
-
Create fragment to main activity as host fragment
What's view or activity is hosting your navigation graph
<fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />
-
The
android:name
attribute contains the class name of your NavHost implementation. -
The
app:navGraph
attribute associates the NavHostFragment with a navigation graph. The navigation graph specifies all of the destinations in this NavHostFragment to which users can navigate. -
The
app:defaultNavHost="true"
attribute ensures that your NavHostFragment intercepts the system Back button. Note that only one NavHost can be the default. If you have multiple hosts in the same layout (two-pane layouts, for example), be sure to specify only one default NavHost.
-
-
Insert fragments to Graph as destination
What is difference between the pop animation and non pop animation? pop refer to what happens when you navigate backward (back stack) not pop is forward