Share
Embed
The Navigation Architecture Component by Laurence de Villers
This is a deck about the new Navigation Architecture Component.
GDG Montreal
June 20, 2018
Transcript
29 slides
-
Challenges for navigation • Passing Arguments • Deep Links •
Fragment Transactions • Testing -
Why navigation component? • Visual editor • Animation between view
• Safe arguments passings • Default arguments • Handling deep links • Automatic setup with BottomNavigationView and side NavigationView -
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. -
Fragmentmanager No need to play with the fragmentmanager, The navigation
view will do the management by itself. -
NavigationUp() will setup class MainActivity : AppCompatActivity() { ... override
fun onSupportNavigateUp() = findNavController(R.id.my_nav_host_fragment).navigateUp() ... } -
Create your graph By default, android studio will find all
the fragment that you have in your application. -
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. -
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) } } -
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> -
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 -
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) } -
DeepLink Do all the logic of stack between starting point
and destination. -
Manifest Deeplink are automatically added when you specify the graph
for an activity. <activity> ... <nav-graph android:value="@navigation/nav_graph"/> </activity> -
Nested Graph Group destinations into a nested graph Separated graph
into groups Move to Nested Graph > New Graph -
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 -
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) -
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