1
Lesson 6:
App navigation
This work is licensed under the Apache 2 license.
Android Development with Kotlin v1.0
This work is licensed under the Apache 2 license.
About this lesson
Lesson 6: App navigation
2
Android Development with Kotlin
This work is licensed under the Apache 2 license.
3
Multiple activities and intents
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Multiple screens in an app
Sometimes app functionality may be separated into multiple screens.
�Examples:
4
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Intent
Requests an action from another app component, such as another Activity
5
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Explicit intent
6
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Explicit intent examples
Navigate between activities in your app:
fun viewNoteDetail() {
val intent = Intent(this, NoteDetailActivity::class.java)
intent.putExtra(NOTE_ID, note.id)
startActivity(intent)
}
7
Navigate to a specific external app:
fun openExternalApp() {
val intent = Intent("com.example.workapp.FILE_OPEN")
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Implicit intent
8
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Implicit intent example
fun sendEmail() {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_EMAIL, emailAddresses)
intent.putExtra(Intent.EXTRA_TEXT, "How are you?")
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
9
Android Development with Kotlin
This work is licensed under the Apache 2 license.
10
App bar, navigation drawer, and menus
Android Development with Kotlin
This work is licensed under the Apache 2 license.
App bar
11
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation drawer
12
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Menu
Define menu items in XML menu resource (located in res/menu folder)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
13
Android Development with Kotlin
This work is licensed under the Apache 2 license.
More menu options
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/menu_gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="@string/menu_slideshow" />
</group>
</menu>
14
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Options menu example
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_intent"
android:title="@string/action_intent" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
15
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Inflate options menu
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}
16
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Handle menu options selected
17
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_intent -> {
val intent = Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY, "pizza")
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
else -> Toast.makeText(this, item.title, Toast.LENGTH_LONG).show()
...
Android Development with Kotlin
This work is licensed under the Apache 2 license.
18
Fragments
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Fragments for tablet layouts
19
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Fragment
20
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Note about fragments
Use the AndroidX version of the Fragment class. (androidx.fragment.app.Fragment).
Don't use the platform version of the Fragment class (android.app.Fragment), which was deprecated.
21
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation within an app
22
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation component
23
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Add dependencies
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
24
In build.gradle, under dependencies:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation host (NavHost)
<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_name"/>
25
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation graph
New resource type located in res/navigation directory
26
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation Editor in Android Studio
27
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Creating a Fragment
28
class DetailFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.detail_fragment, container, false)
}
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Specifying Fragment destinations
29
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Example fragment destination
30
<fragment
android:id="@+id/welcomeFragment"
android:name="com.example.android.navigation.WelcomeFragment"
android:label="fragment_welcome"
tools:layout="@layout/fragment_welcome" >
<action
android:id="@+id/action_welcomeFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation Controller (NavController)
NavController manages UI navigation in a navigation host.
31
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Example NavController
32
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
val navController = findNavController(R.id.myNavHostFragment)
}
fun navigateToDetail() {
navController.navigate(R.id.action_welcomeFragment_to_detailFragment)
}
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
More custom navigation behavior
33
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Passing data between destinations
Using Safe Args:
34
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Setting up Safe Args
In the project build.gradle file:
35
buildscript {
repositories {
google()
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
In the app's or module's build.gradle file:
apply plugin: "androidx.navigation.safeargs.kotlin"
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Sending data to a Fragment
36
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Destination arguments
37
<fragment
android:id="@+id/multiplyFragment"
android:name="com.example.arithmetic.MultiplyFragment"
android:label="MultiplyFragment" >
<argument
android:name="number1"
app:argType="float"
android:defaultValue="1.0" />
<argument
android:name="number2"
app:argType="float"
android:defaultValue="1.0" />
</fragment>
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Supported argument types
38
Type | Type Syntax app:argType=<type> | Supports Default Values | Supports Null 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 + "[]" (for example, "string[]" "long[]") | Yes (only "@null") | Yes |
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.
Supported argument types: Custom classes
39
Type | Type Syntax app:argType=<type> | Supports Default Values | Supports Null Values |
Serializable | Fully qualified class name | Yes (only "@null") | Yes |
Parcelable | Fully qualified class name | Yes (only "@null") | Yes |
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Create action from source to destination
<fragment
android:id="@+id/fragment_input"
android:name="com.example.arithmetic.InputFragment">
<action
android:id="@+id/action_to_multiplyFragment"
app:destination="@id/multiplyFragment" />
</fragment>
40
In nav_graph.xml:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigating with actions
In InputFragment.kt:
41
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setOnClickListener {
val n1 = binding.number1.text.toString().toFloatOrNull() ?: 0.0
val n2 = binding.number2.text.toString().toFloatOrNull() ?: 0.0
val action = InputFragmentDirections.actionToMultiplyFragment(n1, n2)
view.findNavController().navigate(action)
}
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Retrieving Fragment arguments
class MultiplyFragment : Fragment() {
val args: MultiplyFragmentArgs by navArgs()
lateinit var binding: FragmentMultiplyBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val number1 = args.number1
val number2 = args.number2
val result = number1 * number2
binding.output.text = "${number1} * ${number2} = ${result}"
}
}
42
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Navigation UI
43
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Menus revisited
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return item.onNavDestinationSelected(navController) ||
super.onOptionsItemSelected(item)
}
44
Android Development with Kotlin
This work is licensed under the Apache 2 license.
DrawerLayout for navigation drawer
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout" ...>
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/nav_host_fragment" ... />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
app:menu="@menu/activity_main_drawer" ... />
</androidx.drawerlayout.widget.DrawerLayout>
45
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Finish setting up navigation drawer
Connect DrawerLayout to navigation graph:
val appBarConfiguration = AppBarConfig(navController.graph, drawer)
46
Set up NavigationView for use with a NavController:
val navView = findViewById<NavigationView>(R.id.nav_view)
navView.setupWithNavController(navController)
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Understanding the back stack
Back stack
Activity 1
Activity 2
Back stack
Activity 1
Back stack
Activity 2
Activity 1
Activity 2
Activity 3
State 1
State 3
State 2
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Fragments and the back stack
Back stack
Activity 1
Activity 2
Fragment 1
Back stack
Activity 1
Activity 2
Fragment 1
Fragment 2
Back stack
Activity 1
Activity 2
Fragment 1
State 1
State 3
State 2
Android Development with Kotlin
This work is licensed under the Apache 2 license.
49
Summary
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Summary
In Lesson 6, you learned how to:
50
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Learn more
51
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Pathway
52
Android Development with Kotlin
This work is licensed under the Apache 2 license.