The navigation component consists of 3 keywords that are described as below:
1 – Navigation Graph: is a resource file that contains all your destinations and actions. The graph represents all of your app’s navigation paths.
2 – NavHost: is an empty container that displays destinations from your navigation graph. The navigation component contains a default NavHost implementation, NavHostFragment, that displays fragment destinations.
3 – NavController: its an object that manages app navigation within a NavHost. This would help to replace one fragment with another fragment in the NavHost container.
PopTo Not-Inclusive
Pops off everything on the back stack until it finds the referenced fragment transaction.
PopTo Inclusive
Pops off everything on the back stack, including the referenced fragment transaction.
Adding an Overflow Menu
Step 1: Create the new menu resource.
Right click on the res folder within the Android project and select New -> Android Resource File. Name it overflow_menu with resource type menu. Add an About menu item with the ID of the aboutFragment.
Step 2: Call setHasOptionsMenu(true)
This tells Android that our fragment has an options menu, so it will call onCreateOptionsMenu.
Step 3: Override onOptionsItemSelected
Return true if NavigationUI.onNavDestinationSelected returns true, else return super.onOptionsItemSelected.
ATTRIBUTE
|
FUNCTION
|
onOptionsItemSelected
|
Called when a menu item is selected
|
setHasOptionsMenu
|
Tells Android that the Fragment has a menu
|
onCreateOptionsMenu
|
Where you inflate your menu
|
title
|
String displayed in the menu
|
id
|
Used by navigation to determine where to navigate
|
Passing arguments from the Fragment-A to the Fragment-B
Fragments contain arguments in the form of an Android bundle, which is the key value store. A key value store, also known as a dictionary or associative array, is a data structure we use a unique key such as a string to fetch an associated value.
There are limited types that can be stored as value in a bundle. Primitive types such as char, int and float, along with various types of arrays, char sequence, and a few data classes such as arrayList.
Safe Args is a Gradle plugin that generates code to help guarantee that the arguments on both sides match up, while also simplifying argument passing. Because it’s a Gradle plugin, using it is a two-step process in terms of what to add to your Gradle files.
Do'stlaringiz bilan baham: |