such as - Fragments - Activities - Navigation graphs and subgraphs - Custom destination types These are connected by actions which together with destinations make up an apps navigation graph
back actions by default - Standard resources for animations and transitions - First class deep link handling - Bottom navigation and drawers - Type safety for arguments - Navigation Editor
state should be represented by a stack of destinations - The Up button never exits your app - Up and back are identical within your app's task - Deep linking and navigating to a destination should yield the same stack
you want to use Navigation with Android studio you must use Android Studio 3.3 or higher. Add the dependencies to build.gradle dependencies { def nav_version = "1.0.0-beta02" implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version" implementation "android.arch.navigation:navigation-ui-ktx:$nav_version" }
android:name="ie.redstar.navigating.TuamFragment" android:label="fragment_tuam" tools:layout="@layout/fragment_tuam"/> </navigation> The XML contains the same id, name, label, and layout attributes for the destination as the ui editor
indicate the first screen users see. - Known as the start destination - Change by right clicking and Set as Start Destination - Or click Assign start destination
is done using the NavController class. A NavController can be retrieved using one of the following static methods: - NavHostFragment.findNavController(Fragment) - Navigation.findNavController(Activity, @IdRes int viewId) - Navigation.findNavController(View) val button = root.findViewById<Button>(R.id.mullingar_button) button.setOnClickListener { view -> view.findNavController().navigate( R.id.action_tuamFragment_to_mullingarFragment1) }
NavigationUI class. This contains static methods that manage navigation with - Top app bar - Navigation drawer - Bottom navigation NavController is responsible for replacing contents of the NavHost. UI elements outside the NavHost also need to be updated
the NavController’s current destination or its arguments changed. navController.addOnDestinationChangedListener { navController, destination, arguments -> textView.setText(destination.label) }
automatically provide up navigation and provide a text from android:label in our nav_graph private lateinit var appBarConfiguration: AppBarConfiguration override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navController = findNavController(R.id.fragment) appBarConfiguration = AppBarConfiguration(navController.graph) setupActionBarWithNavController(navController, appBarConfiguration) }
as fragment as in nav_graph.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@id/tuamFragment" android:icon="@drawable/ic_adb_white_24dp" android:title="@string/tuam"/> ... </menu>
setupWithNavController() and thats it. val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottom_nav) bottomNavigation.setupWithNavController(navController)