Lesson 6 - App Navigation
Lesson 6 - App Navigation
App navigation
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)
This work is licensed under the
Android Development with Kotlin Apache 2 license. 4
Intent
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)
}
}
<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>
This work is licensed under the
Android Development with Kotlin Apache 2 license. 13
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>
This work is licensed under the
Android Development with Kotlin Apache 2 license. 14
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>
This work is licensed under the
Android Development with Kotlin Apache 2 license. 15
Inflate options menu
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()
...
This work is licensed under the
Android Development with Kotlin Apache 2 license. 17
Fragments
In build.gradle, under
dependencies:
implementation "androidx.navigation:navigation-fragment-ktx:
$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
<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"/>
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" />
This work is licensed under the
</fragment> Android Development with Kotlin Apache 2 license. 30
Navigation Controller
(NavController)
NavController manages UI navigation in a navigation
host.
● Specifying a destination path only names the action,
but it doesn’t execute it.
● To follow a path, use NavController.
navController.navigate(R.id.action_welcomeFragment_to_detailFragment)
}
}
<action
android:id="@+id/action_to_multiplyFragment"
app:destination="@id/multiplyFragment" />
</fragment>
val action =
InputFragmentDirections.actionToMultiplyFragment(n1, n2)
view.findNavController().navigate(action)
}
} This work is licensed under the
Android Development with Kotlin Apache 2 license. 41
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}"
}
<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>
This work is licensed under the
Android Development with Kotlin Apache 2 license. 45
Finish setting up navigation
drawer
Connect DrawerLayout to navigation graph:
Activity 3
Fragment 2