Lesson 6 - App Navigation
Lesson 6 - App Navigation
App navigation
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 6: App navigation
● Multiple activities and intents
● App bar, navigation drawer, and menus
● Fragments
● Navigation in an app
● More custom navigation behavior
● Navigation UI
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Multiple activities and
intents
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Multiple screens in an app
Sometimes app functionality may be separated into multiple screens.
Examples:
● View details of a single item (for example, product in a shopping app)
● Create a new item (for example, new email)
● Show settings for an app
● Access services in other apps (for example, photo gallery or browse
documents)
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Intent
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Explicit intent
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Explicit intent examples
Navigate between activities in your app:
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Implicit intent
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Implicit intent example
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
App bar, navigation drawer,
and menus
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
App bar
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Navigation drawer
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Menu
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
More menu options
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Options menu example
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Inflate options menu
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Handle menu options selected
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Fragments
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Fragments for tablet layouts
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Fragment
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Note about fragments
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Navigation within an app
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Navigation component
● Collection of libraries and tooling, including an integrated editor, for
creating navigation paths through an app
● Assumes one Activity per graph with many Fragment
destinations
● Consists of three major parts:
○ Navigation graph
○ Navigation Host (NavHost)
○ Navigation Controller (NavController)
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Add dependencies
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Navigation host (NavHost)
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Navigation graph
New resource type located in res/navigation
directory
● XML file containing all of your navigation
destinations and actions
● Lists all the (Fragment/Activity) destinations that
can be navigated to
● Lists the associated actions to traverse between
them
● Optionally lists animations for entering or exiting
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Navigation Editor in Android Studio
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Creating a Fragment
● Extend Fragment class
● Override onCreateView()
● Inflate a layout for the Fragment that you have defined in XML
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Specifying Fragment destinations
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Example fragment destination
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Navigation Controller (NavController)
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Example NavController
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
More custom navigation
behavior
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Passing data between destinations
Using Safe Args:
● Ensures arguments have a valid type
● Lets you provide default values
● Generates a <SourceDestination>Directions class with methods for
every action in that destination
● Generates a class to set arguments for every named action
● Generates a <TargetDestination>Args class providing access to the
destination's arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Setting up Safe Args
In the project build.gradle file:
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Sending data to a Fragment
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Destination arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Supported argument types
Type Type Syntax Supports Default Supports Null
app:argType=<type> Values Values
Integer "integer" Yes No
Float "float" Yes No
Long "long" Yes No
Boolean "boolean" Yes ("true" or "false") No
String "string" Yes Yes
Array above type + "[]" Yes (only "@null") Yes
(for example, "string[]" "long[]")
Enum Fully qualified name of the enum Yes No
Resource reference "reference" Yes No
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Supported argument types: Custom classes
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Create action from source to destination
In nav_graph.xml:
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Navigating with actions
In InputFragment.kt:
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Retrieving Fragment arguments
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Navigation UI
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Menus revisited
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
DrawerLayout for navigation drawer
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Finish setting up navigation drawer
Connect DrawerLayout to navigation graph:
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Understanding the back stack
State 1 State 2 State 3
Activity 3
Android Development with Kotlin This work is licensed under the Apache 2 license.
Fragments and the back stack
State 1 State 2 State 3
Fragment 2
Android Development with Kotlin This work is licensed under the Apache 2 license.
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Summary
In Lesson 6, you learned how to:
● Use explicit and implicit intents to navigate between Activities
● Structure apps using fragments instead of putting all UI code in the
Activity
● Handle navigation with NavGraph, NavHost, and NavController
● Use Safe Args to pass data between fragment destinations
● Use NavigationUI to hook up top app bar, navigation drawer, and bottom
navigation
● Android keeps a back stack of all the destinations you’ve visited, with
each new destination being pushed onto the stack.
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Learn more
● Principles of navigation
● Navigation component
● Pass data between destinations
● NavigationUI
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 52