Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Navigation Architecture Component by Lauren...

The Navigation Architecture Component by Laurence de Villers

This is a deck about the new Navigation Architecture Component.

GDG Montreal

June 20, 2018
Tweet

More Decks by GDG Montreal

Other Decks in Technology

Transcript

  1. Why navigation component? • Visual editor • Animation between view

    • Safe arguments passings • Default arguments • Handling deep links • Automatic setup with BottomNavigationView and side NavigationView
  2. Navigation component is scoped to a single Activity WHY ?

    - Activities are limited for animations You can have a fragment that will start another activity with another flow of navigation component.
  3. NavigationUp() will setup class MainActivity : AppCompatActivity() { ... override

    fun onSupportNavigateUp() = findNavController(R.id.my_nav_host_fragment).navigateUp() ... }
  4. Create your graph By default, android studio will find all

    the fragment that you have in your application.
  5. Starting destination Destinations Action is the route between two fragments

    with all the details for animations, destination and popUpTo Define the argument that this fragment will received and an default value for it.
  6. Automatic setup with BottomNavigationView / NavigationView Use the same ID

    in your app:menu of the one in the navigation graph. class MainActivity : AppCompatActivity() { … private fun setupBottomNavigationView() { val navController = findNavController(R.id.my_nav_host_fragment) bottomNavigationView?.setupWithNavController(navController) } }
  7. DrawerLayout and navigation Make ActionBar and drawers • No need

    of onNavigationItemSelected private fun setupNavigationMenu(navController: NavController) { NavigationUI.setupWithNavController(navigationView, navController) } private fun setupActionBar(navController: NavController) { drawerLayout = findViewById(R.id.drawer_layout) NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout) } <android.support.v4.widget.DrawerLayout > <LinearLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" /> <fragment android:id="@+id/my_nav_host_fragment"/> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/nav_view" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout>
  8. Safe arguments It generates a class named [Destination Fragment Name]Args.

    With actions override fun onViewCreated(view: View, savedInstanceState: Bundle?) { arguments?.let { actionTextView.text = getArguments()?.getString("ARG_ACTION1"); } } v.findViewById<Button>(R.id.button)?.setOnClickListener { val navController = v.findNavController() val bundle = Bundle().also { it.putString("ARG_ACTION1","GDG is cool" ) } navController.navigate(R.id.action_home_to_action1, bundle) } Before
  9. Safe arguments It generates two class : One class named

    [Destination Fragment Name]Args. With actions The other one class named [Current fragment name]Directions. override fun onViewCreated(view: View, savedInstanceState: Bundle?) { arguments?.let { val passedArguments = Action1Args.fromBundle(it) actionTextView.text = passedArguments.ARG_ACTION1 } } v.findViewById<Button>(R.id.button)?.setOnClickListener { val navController = v.findNavController() val action = HomeDirections.action_home_to_action1() action.setARG_ACTION1("GDG is COOL") navController.navigate(action) }
  10. Manifest Deeplink are automatically added when you specify the graph

    for an activity. <activity> ... <nav-graph android:value="@navigation/nav_graph"/> </activity>
  11. Nested Graph Group destinations into a nested graph Separated graph

    into groups Move to Nested Graph > New Graph
  12. ALPHA !!!!!!! • A lots of small issues need to

    be fix Ex : Issues with toolbar arrow navigation • WILL CHANGE WITH THE COMMENTS OF APHA USER • NOT API STABLE
  13. Migrate to the Navigation Architecture Component Activity A Activity B

    Nav Graph A Nav Graph B Link activities by adding an activity destination in the graph. To navigate to another activity use ActivityNavigator ActivityNavigator(Context context).navigate(ActivityNavigator.Destination destination, Bundle args, NavOptions navOptions)
  14. Ressources See issues : https://issuetracker.google.com/issues go Navigation and give feedback

    Podcast : http://androidbackstage.blogspot.com/ The Episode Navigation Controller Documentation : https://developer.android.com/topic/libraries/architecture/navigation/navigation-principles Code lab : https://codelabs.developers.google.com/codelabs/android-navigation