SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
Android JetPack: Easy
navigation with the new
Navigation Controller
Leonardo Pirro
Android Developer @ IQUII
@lpirro93
Who Am I?
Hi, I’m Leo. Android Developer @ IQUII
The Navigation
Problem
The Navigation Problem
• Android Framework never had a concise and
real solution to manage a common problem
like Navigation
• Navigation is “hard” and there are a lot things
that you have to solve by your own
• A lot of boilerplate code
= A LOT of bugs and crash
Android JetPack to the rescue
The Navigation
Architecture Component
What Navigation is trying to solve
Fragment Transactions
Testing
Deep Links
Passing Arguments
Up and Back
Navigation Graph
A navigation graph is a blueprint of
possible navigation destinations and the
actions that link them.
Navigation Graph
Define the
Navigation Graph
Navigation Graph
The navigation Graph is defined under res/navigation/navgraph_name.xml
Navigation Graph
<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"
app:startDestination="@id/signinFragment">
...
</navigation>
• <navigation> is the root node of the navigation graph
• <navigation> can contains one or more destination represented by
<activity> or <fragment>
• app:startDestination is an attribute which controls what destination is
launched by default when the user first opens the app.
Adding a new destination
Navigation Graph
<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"
app:startDestination="@id/signinFragment">
<fragment
android:id="@+id/homeFragment"
android:name=“com.lpirro.navigation.HomeFragment”
tools:layout="@layout/fragment_home" />
</navigation>
• android:id is the ID of the fragment that you can use to destination elsewhere in xml
or code
• android:name is your fragment class that will be instantiated when you navigate to
that destination
• tools:layout defines the layout xml file that should be shown in the graph editor
The role of the Activity
The role of the Activity
An entry-point to your app
The activity manages any global navigation
(BottomNavigation, NavigationDrawer, etc), but
delegates to a NavHost for content
The role of the Activity
The activity manages any global navigation
(BottomNavigation, NavigationDrawer, etc), but
delegates to a NavHost for content
The role of the Activity
The activity manages any global navigation
(BottomNavigation, NavigationDrawer, etc), but
delegates to a NavHost for content
<fragment
android:id="@+id/navHostHome"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="0dp" />
activity_main.xml
activity_main.xml
<fragment
android:id="@+id/navHostHome"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="0dp" />
android:name defines the fragment class of the NavHostFragment
from the navigation-fragment dependency
activity_main.xml
<fragment
android:id="@+id/navHostHome"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="0dp" />
android:navGraph associates a navigation graph to the
NavHostFragment
activity_main.xml
<fragment
android:id="@+id/navHostHome"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="0dp" />
android:defaultNavHost=“true” connects the system back
button to the NavHostFragment
Navigate to a destination
Navigate to a destination
buttonQuiz.setOnClickListener {
view.findNavController().navigate(R.id.quizFragment)
}
Navigate to a destination
val options = NavOptions.Builder()
.setEnterAnim(R.anim.slide_in_right)
.setExitAnim(R.anim.slide_out_left)
.setPopEnterAnim(R.anim.slide_in_left)
.setPopExitAnim(R.anim.slide_out_right)
.build()
buttonQuiz.setOnClickListener {
view.findNavController().navigate(R.id.quizFragment, null, options)
}
navigation-ui
A set of static methods to connects
Navigation with Material Design UI
components
BottomNavigation example
MainActivity.kt
bottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.homeFragment -> {
navController.navigate(R.id.homeFragment)
true }
R.id.quizFragment -> {
navController.navigate(R.id.quizFragment)
true }
else -> false
}
}
MainActivity.kt
bottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.homeFragment -> {
navController.navigate(R.id.homeFragment)
true }
R.id.quizFragment -> {
navController.navigate(R.id.quizFragment)
true }
else -> false
}
}
BAD
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
...>
<android.support.v7.widget.Toolbar
... />
<fragment
android:id="@+id/navHostHome"
android:name=“androidx.navigation.fragment.NavHostFragment"
...
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
app:menu="@menu/bottom_navigation_main" />
</android.support.constraint.ConstraintLayout>
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
...>
<android.support.v7.widget.Toolbar
... />
<fragment
android:id="@+id/navHostHome"
android:name=“androidx.navigation.fragment.NavHostFragment"
...
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
app:menu="@menu/bottom_navigation_main" />
</android.support.constraint.ConstraintLayout>
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
...>
<android.support.v7.widget.Toolbar
... />
<fragment
android:id="@+id/navHostHome"
android:name=“androidx.navigation.fragment.NavHostFragment"
...
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
app:menu="@menu/bottom_navigation_main" />
</android.support.constraint.ConstraintLayout>
Uses the same IDs of the
destinations defined in the
Navigation Graph
res/layout/activity_main.xml KF 🐼
<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"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/quizFragment"
android:name=“com.lpirro.navigationtalk.QuizFragment"
android:label="Quiz"
tools:layout="@layout/fragment_quiz" >
</fragment>
<fragment
android:id="@+id/homeFragment"
android:name=“com.lpirro.navigationtalk.HomeFragment"
android:label="Home" />
<fragment
android:id="@+id/filmFragment"
android:name=“com.lpirro.navigationtalk.FilmFragment"
android:label="Film" />
<fragment
android:id="@+id/cinemaFragment"
res/navigation/nav_graph.xml
res/menu/bottomnav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/homeFragment"
android:icon="@drawable/ic_nav_home"
android:title="@string/nav_home" />
<item
android:id="@+id/filmFragment"
android:icon="@drawable/ic_nav_movies"
android:title="@string/nav_movies" />
<item
android:id="@+id/cinemaFragment"
android:icon="@drawable/ic_nav_theatre"
android:title="@string/nav_cinema" />
<item
android:id="@+id/quizFragment"
android:icon="@drawable/ic_nav_quiz"
android:title="@string/nav_quiz"/>
</menu>
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = Navigation.findNavController(this, R.id.navHost)
NavigationUI.setupWithNavController(toolbar, navController)
bottomNavigationView.setupWithNavController(navController)
}
Actions
Create an action
Create an action
<fragment
android:id="@+id/homeFragment"
android:name=“com.lpirro.app.home.HomeFragment"
android:label="HomeFragment" >
<action
android:id="@+id/action_homeFragment_to_articleDetailFragment"
app:destination="@id/articleDetailFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim=“@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
Navigate using an action
button.setOnClickListener {
it.findNavController()
.navigate(R.id.action_signinFragment_to_homeFragment)
}
Safe Args
Passing data via SafeArgs
<fragment
android:id="@+id/articleDetailFragment"
android:name=“com.lpirro.app.articledetail.ArticleDetailFragment”
android:label="ArticleDetailFragment" >
<argument
android:name="article"
app:argType=“com.lpirro.app.data.models.Article” />
</fragment>
Passing data via SafeArgs
HomeFragment.kt
val directions = HomeFragmentDirections
.actionHomeFragmentToArticleDetailFragment(article)
view?.findNavController()?.navigate(directions)
ArticleDetailFragment.kt
article = ArticleDetailFragmentArgs.fromBundle(arguments).article
Deep links
Explicit
Notifications
App widgets
App shortcut
Actions
Implicit
Web URLs
Custom scheme URIs
Explicit deep links
With NavDeepLinkBuilder
Link to a specific destination in your app by its ID
MyFirebaseService.kt
val bundle = Bundle()
bundle.putParcelable("article", Article(id))
val deepLinkBuilder = NavDeepLinkBuilder(this)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.articleDetailFragment)
.setArguments(bundle)
val pendingIntent = deepLinkBuilder.createPendingIntent()
Implicit deep links
With <deeplink> in your navigation graph
Implicit deep links
nav_graph.xml
<fragment
android:id="@+id/quizFragment"
android:name=“com.lpirro.navigationtalk.QuizFragment”
android:label="Quiz"
tools:layout="@layout/fragment_quiz" >
<deepLink app:uri="www.example.com"/>
</fragment>
nav_graph.xml
<fragment
android:id="@+id/quizFragment"
android:name=“com.lpirro.navigationtalk.QuizFragment”
android:label="Quiz"
tools:layout="@layout/fragment_quiz" >
<deepLink app:uri="www.example.com"/>
<deepLink app:uri="www.example.com/content/{myArgs}"/>
</fragment>
nav_graph.xml
<fragment
android:id="@+id/quizFragment"
android:name=“com.lpirro.navigationtalk.QuizFragment”
android:label="Quiz"
tools:layout="@layout/fragment_quiz" >
<deepLink app:uri="www.example.com"/>
<deepLink app:uri=“www.example.com/content/{myArgs}"/>
<deepLink app:uri=“android-app://example/content/{id}”/>
</fragment>
http://www.androiddevs.it
Slack community - Android Developers Italia
Keep in touch!
lpirro
lpirro93
linkedin.com/in/lpirro
medium.com/@lpirro
Thank
You
Leonardo Pirro
Android Developer @ IQUII
@lpirro93

Weitere ähnliche Inhalte

Was ist angesagt?

Android animation
Android animationAndroid animation
Android animation
Krazy Koder
 
Android resource
Android resourceAndroid resource
Android resource
Krazy Koder
 

Was ist angesagt? (20)

Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Try Jetpack Compose
Try Jetpack ComposeTry Jetpack Compose
Try Jetpack Compose
 
오픈소스의 이해와 활용
오픈소스의 이해와 활용오픈소스의 이해와 활용
오픈소스의 이해와 활용
 
An introduction to Vue.js
An introduction to Vue.jsAn introduction to Vue.js
An introduction to Vue.js
 
Android animation
Android animationAndroid animation
Android animation
 
Android-dialogs in android-chapter14
Android-dialogs in android-chapter14Android-dialogs in android-chapter14
Android-dialogs in android-chapter14
 
React hooks
React hooksReact hooks
React hooks
 
React workshop
React workshopReact workshop
React workshop
 
Gitflow - Branching and Merging Flow for Git
Gitflow - Branching and Merging Flow for GitGitflow - Branching and Merging Flow for Git
Gitflow - Branching and Merging Flow for Git
 
routing.pptx
routing.pptxrouting.pptx
routing.pptx
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
Android resource
Android resourceAndroid resource
Android resource
 
Angular
AngularAngular
Angular
 
Introduzione ad angular 7/8
Introduzione ad angular 7/8Introduzione ad angular 7/8
Introduzione ad angular 7/8
 
Reactjs
ReactjsReactjs
Reactjs
 
Angular components
Angular componentsAngular components
Angular components
 
Starting with Git & GitHub
Starting with Git & GitHubStarting with Git & GitHub
Starting with Git & GitHub
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 

Ähnlich wie Android JetPack: easy navigation with the new Navigation Controller

Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9
Gustavo Fuentes Zurita
 
Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9
Gustavo Fuentes Zurita
 
Android app development basics
Android app development basicsAndroid app development basics
Android app development basics
Anton Narusberg
 

Ähnlich wie Android JetPack: easy navigation with the new Navigation Controller (20)

Android Workshop
Android WorkshopAndroid Workshop
Android Workshop
 
android level 3
android level 3android level 3
android level 3
 
Fragments: Why, How, What For?
Fragments: Why, How, What For?Fragments: Why, How, What For?
Fragments: Why, How, What For?
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development Practices
 
Design Patterns for Tablets and Smartphones
Design Patterns for Tablets and SmartphonesDesign Patterns for Tablets and Smartphones
Design Patterns for Tablets and Smartphones
 
Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9
 
Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9Androidoscon20080721 1216843094441821-9
Androidoscon20080721 1216843094441821-9
 
Android app development basics
Android app development basicsAndroid app development basics
Android app development basics
 
phonegap with angular js for freshers
phonegap with angular js for freshers    phonegap with angular js for freshers
phonegap with angular js for freshers
 
Building iPad apps with Flex - 360Flex
Building iPad apps with Flex - 360FlexBuilding iPad apps with Flex - 360Flex
Building iPad apps with Flex - 360Flex
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android Apps
 
Data binding 入門淺談
Data binding 入門淺談Data binding 入門淺談
Data binding 入門淺談
 
Getting Started With Material Design
Getting Started With Material DesignGetting Started With Material Design
Getting Started With Material Design
 
Lecture #1 Creating your first android project
Lecture #1  Creating your first android projectLecture #1  Creating your first android project
Lecture #1 Creating your first android project
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
What's New in Android
What's New in AndroidWhat's New in Android
What's New in Android
 
Invading the home screen
Invading the home screenInvading the home screen
Invading the home screen
 
Material Design (The Technical Essentials) by Mohammad Aljobairi @AMMxDROID
Material Design (The Technical Essentials) by Mohammad Aljobairi @AMMxDROIDMaterial Design (The Technical Essentials) by Mohammad Aljobairi @AMMxDROID
Material Design (The Technical Essentials) by Mohammad Aljobairi @AMMxDROID
 
Android Materials Design
Android Materials Design Android Materials Design
Android Materials Design
 
Chapter 5 - Layouts
Chapter 5 - LayoutsChapter 5 - Layouts
Chapter 5 - Layouts
 

Kürzlich hochgeladen

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Kürzlich hochgeladen (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 

Android JetPack: easy navigation with the new Navigation Controller