Slide 1

Slide 1 text

Navigating the Navigation Component Hugo Visser @botteaap [email protected]

Slide 2

Slide 2 text

Navigation as a concept, declarative at the code level. “Principles of Navigation” and Navigation UI that implements this Decoupling of navigation from Activities and Fragments with NavController What the Navigation Component provides

Slide 3

Slide 3 text

Why would you need this Decouple navigation relations between destinations from code Modularise features including navigation to and from those features Reduce code to implement “Principles of Navigation”

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Navigation supportFragmentManager.beginTransaction() .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) .replace(R.id.fragment, MainFragment()) .commit()

Slide 6

Slide 6 text

Navigation startActivity(MyActivity.createIntent(context, ...)) MyActivity.show(this)

Slide 7

Slide 7 text

Defining navigation Navigation Graph w/ Android Studio editor Organise navigation flows in graphs New resource type: @navigation/my_navigation_graph

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Navigation Graph Define Destinations w/ their ids Every graph has a start destination Define actions to navigate between destinations Specify transitions and backstack behaviour on actions Deeplinks

Slide 10

Slide 10 text

Implementing Navigation You can implement Navigator and Destination for you own purposes Default implementation: Fragments, many improvements made in androidx. ActivityDestination is also provided Single activity to host NavHostFragment

Slide 11

Slide 11 text

SINGLE ACTIVITY?

Slide 12

Slide 12 text

Impact on your app architecture https://www.youtube.com/watch?v=2k8x8V77CrU

Slide 13

Slide 13 text

Sharing is caring private val viewModel: EventViewModel by activityViewModels { viewModelFactory }

Slide 14

Slide 14 text

NavHostFragment

Slide 15

Slide 15 text

Navigate // From your Fragment you can Navigate (not from your ViewModel!) // Navigate by id, findNavController is an extension provided by // navigation-fragment-ktx findNavController().navigate(R.id.event_list) // Go back findNavController().popBackStack() // Navigate with Directions findNavController().navigate(AttendeeListFragmentDirections.actionEventList())

Slide 16

Slide 16 text

SafeArgs Gradle plugin for type safe passing of arguments Generates Kotlin or Java code Generates Direction classes, representing (global) actions Prefer Directions over using ids for navigation

Slide 17

Slide 17 text

Using args class MainFragment: Fragment() { private val args by navArgs() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) doSomethingWithPersonId(args.personId) } }

Slide 18

Slide 18 text

Navigation UI “Principles of Navigation” -- Do the right thing with less code React on navigation state changes → OnDestinationChangeListener Hook up existing UI to navigation

Slide 19

Slide 19 text

Navigation UI: click listeners override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) view.findViewById(R.id.navigate).setOnClickListener { Navigation.createNavigateOnClickListener(MyFragmentDirections.myAction()) } }

Slide 20

Slide 20 text

Navigation UI @NonNull public static View.OnClickListener createNavigateOnClickListener( @NonNull final NavDirections directions) { return new View.OnClickListener() { @Override public void onClick(View view) { findNavController(view).navigate(directions); } }; }

Slide 21

Slide 21 text

Navigation UI: Toolbar & friends override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Use the built-in ActionBar without a drawer setupActionBarWithNavController(findNavController(R.id.nav_host_fragment), null) }

Slide 22

Slide 22 text

Navigation UI: Toolbar & friends override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navController = findNavController(R.id.nav_host_fragment) // Configure the top level destinations (e.g. no back button) val appBarConfiguration = AppBarConfiguration(setOf(R.id.main_fragment, R.id.second_fragment)) findViewById(R.id.toolbar) .setupWithNavController(navController, appBarConfiguration) }

Slide 23

Slide 23 text

Navigation UI: Toolbar & friends

Slide 24

Slide 24 text

Navigation UI With AppBarConfiguration with DrawerLayout Manage drawer icon (hamburger vs back button) NavView & other menu items

Slide 25

Slide 25 text

Navigation UI

Slide 26

Slide 26 text

Navigation UI

Slide 27

Slide 27 text

Navigation UI: BottomNavigationView “Principles of Navigation”

Slide 28

Slide 28 text

Navigation UI: BottomNavigationView Default implementation is “Android way” on material.io https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSam ple Requires support for multiple backstacks in Fragments

Slide 29

Slide 29 text

Current limitations (?) Navigate to DialogFragments Overlays → navigation always does a “replace” Return a result like startActivityForResult → modify ViewModel state Nothing specific provided for “master-detail”; nesting NavHostFragments works.

Slide 30

Slide 30 text

Further exploration https://developer.android.com/guide/navigation https://www.youtube.com/watch?v=2k8x8V77CrU

Slide 31

Slide 31 text

Thanks @botteaap [email protected] speakerdeck.com/hugovisser