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

Android Jetpack - Navigation Components

Android Jetpack - Navigation Components

2018년 8월 12일 GDG 주최 Google I/O 2018 Extended Android Overflow 행사 발표자료 입니다. Android Jetpack의 Navigation Components를 다루었습니다.

Seungmin 마량

August 12, 2018
Tweet

More Decks by Seungmin 마량

Other Decks in Technology

Transcript

  1. Challenges for Navigation - Fragment Transaction - Up and Back

    - Passing Arguments - Deep Links - Error-prone Boilerplate
  2. <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" app:startDestination="@+id/title_screen"> <fragment android:id="@+id/title_screen" android:name="com.example.android.navigationsample.TitleScreen" android:label="fragment_title_screen" tools:layout="@layout/fragment_title_screen">

    <action android:id="@+id/action_title_screen_to_register" app:destination="@id/register" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left"/> </fragment> </navigation> nav_graph.xml
  3. <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" app:startDestination="@+id/title_screen"> <fragment android:id="@+id/title_screen" android:name="com.example.android.navigationsample.TitleScreen" android:label="fragment_title_screen" tools:layout="@layout/fragment_title_screen">

    <action android:id="@+id/action_title_screen_to_register" app:destination="@id/register" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left"/> </fragment> </navigation> nav_graph.xml Destination
  4. <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" app:startDestination="@+id/title_screen"> <fragment android:id="@+id/title_screen" android:name="com.example.android.navigationsample.TitleScreen" android:label="fragment_title_screen" tools:layout="@layout/fragment_title_screen">

    <action android:id="@+id/action_title_screen_to_register" app:destination="@id/register" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left"/> </fragment> </navigation> nav_graph.xml Action
  5. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) val navController = findNavController(R.id.nav_host) NavigationUI.setupActionBarWithNavController(this, navController) } override fun onSupportNavigateUp() = findNavController(R.id.nav_host).navigateUp() } MainActivity.kt
  6. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) val navController = findNavController(R.id.nav_host) NavigationUI.setupActionBarWithNavController(this, navController) } override fun onSupportNavigateUp() = findNavController(R.id.nav_host).navigateUp() } MainActivity.kt
  7. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) val navController = findNavController(R.id.nav_host) NavigationUI.setupActionBarWithNavController(this, navController) } override fun onSupportNavigateUp() = findNavController(R.id.nav_host).navigateUp() } MainActivity.kt
  8. btnGame.setOnClickListener { view -> view.findNavController().navigate(R.id.gameFragment) } MainFragment.kt btnGame.setOnClickListener { view

    -> view.findNavController().navigate(R.id.action_mainFragment_to_gameFragment) } btnGame.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action _mainFragment_to_gameFragment))
  9. Passing Arguments arguments?.let { val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text =

    arguments.screenName txt_name.text = arguments.userName } val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions)
  10. val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions) Passing Arguments arguments?.let {

    val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text = arguments.screenName txt_name.text = arguments.userName }
  11. val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions) Passing Arguments arguments?.let {

    val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text = arguments.screenName txt_name.text = arguments.userName } Default Valueо ࢸ੿غ૑ ঋ਷ ScreenName
  12. Passing Arguments arguments?.let { val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text =

    arguments.screenName txt_name.text = arguments.userName } val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions)
  13. Getting Arguments arguments?.let { val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text =

    arguments.screenName txt_name.text = arguments.userName } val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions)
  14. arguments?.let { val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text = arguments.screenName txt_name.text

    = arguments.userName } Getting Arguments val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions)
  15. arguments?.let { val arguments = GameFragmentArgs.fromBundle(it) txt_screen_name.text = arguments.screenName txt_name.text

    = arguments.userName } Getting Arguments val directions = MainFragmentDirections.actionToGame("GameFragment") directions.setUserName("rfrost") view.findNavController().navigate(directions)
  16. Explicit Implicit Notification জ ই੉௑ জ ਤઇ Slices Web URLs

    Custom Scheme URLs জ ղࠗীࢲ ੉ز জ ৻ࠗীࢲ ੉ز
  17. <fragment android:id="@+id/resultFragment" android:name="com.rfrost.navigationsample.ResultFragment" android:label="ResultFragment" tools:layout="@layout/result_fragment"> <deepLink android:autoVerify="true" app:uri="http://www.google.com" /> <deepLink

    app:uri="navsample://result/userName/{userName}" /> <argument android:name="userName" android:defaultValue="name" app:argType="string" /> </fragment> ImplicitDeepLink.kt
  18. <fragment android:id="@+id/resultFragment" android:name="com.rfrost.navigationsample.ResultFragment" android:label="ResultFragment" tools:layout="@layout/result_fragment"> <deepLink android:autoVerify="true" app:uri="http://www.google.com" /> <deepLink

    app:uri="navsample://result/userName/{userName}" /> <argument android:name="userName" android:defaultValue="name" app:argType="string" /> </fragment> ImplicitDeepLink.kt
  19. <fragment android:id="@+id/resultFragment" android:name="com.rfrost.navigationsample.ResultFragment" android:label="ResultFragment" tools:layout="@layout/result_fragment"> <deepLink android:autoVerify="true" app:uri="http://www.google.com" /> <deepLink

    app:uri="navsample://result/userName/{userName}" /> <argument android:name="userName" android:defaultValue="name" app:argType="string" /> </fragment> ImplicitDeepLink.kt
  20. ই૒ ࠛউ੿ೠ ࢎ೦ - BackActionীࢲ NavControllerҗ ActionBar੄ زӝചо ੉࢚ೣ -

    Manifestীࢲ nav-graphо ੜ ز੘ೞ૑ ঋ਺ - Deeplinkীࢲ ઺р BackStackਸ ݽف ࢓ܻ૑ ޅೣ - Deeplinkীࢲ {arguments}ח String݅ ؽ