Slide 1

Slide 1 text

Architecture Components ษڧձ ͋Μ͍͟Ώ͖ ͕͑Θ Θ͞ͼʔ; ͋Β͖ ୈ8ճ໨ Navigation

Slide 2

Slide 2 text

Architecture Components ษڧձ ͱ͸ • Architecture Components ͷجૅ஌ࣝʹֶ͍ͭͯͿษڧձ • ओ࠵ɿGDG Tokyo ϋογϡλά: #gdgtokyo • ߨࢣɺνϡʔλʔɿGoogle ࣾһͱ Google Developer Expert • ʢ͋Μ͍͟Ώ͖ɺ ͕͑ΘɺΘ͞ͼʔ;ɺ͋Β͖ʣ • ౰೔νϡʔλʔ : @95kugo, @STAR_ZERO, @tomoya0x00

Slide 3

Slide 3 text

Architecture Components ษڧձ ͱ͸ • ܭ4ճͷ༧ఆ • ୈ5ճ: Lifecycle, ViewModel, LiveData ͷ෮श • ୈ6ճ: Room ͷ෮श, Paging • ୈ7ճ: WorkManager • ୈ8ճ: Navigation λΠϜςʔϒϧ 19:30ʙ19:35 ѫࡰˍઆ໌ 19:35ʙ20:15 આ໌ 20:15ʙ20:25 ٳܜ 20:25ʙ ՝୊औΓ૊Έ

Slide 4

Slide 4 text

Architecture Components ͱ͸ ෳ਺ͷػೳɾϥΠϒϥϦͷ૯শ • Data Binding • Lifecycles • LiveData • ViewModel • Room • Paging • WorkManager • Navigation

Slide 5

Slide 5 text

Architecture Components ͱ͸ ෳ਺ͷػೳɾϥΠϒϥϦͷ૯শ • Data Binding • Lifecycles • LiveData • ViewModel • Room • Paging • WorkManager • Navigation ݸผར༻OK ૊Έ߹Θͤར༻OK

Slide 6

Slide 6 text

Architecture Components ͱ͸ ෳ਺ͷػೳɾϥΠϒϥϦͷ૯শ • Data Binding • Lifecycles • LiveData • ViewModel • Room • Paging • WorkManager • Navigation ݸผར༻OK ૊Έ߹Θͤར༻OK #1 #2 #3 #4

Slide 7

Slide 7 text

Architecture Components ͱ͸ ෳ਺ͷػೳɾϥΠϒϥϦͷ૯শ • Data Binding • Lifecycles • LiveData • ViewModel • Room • Paging • WorkManager • Navigation ݸผར༻OK ૊Έ߹Θͤར༻OK #1 #2 #3 #4 #5 #6 #7 #8

Slide 8

Slide 8 text

Architecture Components ͷ໨త ݎ࿚ (robust) Ͱ ςετ͠΍͘͢ (testableʣ อक͠΍͍͢ (maintainable) ΞϓϦͷઃܭ͕Ͱ͖ΔΑ͏ʹखॿ͚͢Δ

Slide 9

Slide 9 text

Navigation

Slide 10

Slide 10 text

Navigation • Fragment Transaction ͷ؆қԽ • Ξχϝʔγϣϯ • σΟʔϓϦϯΫ • BottomNavigation / Toolbar ͳͲͱ؆୯ʹ࿈ܞ • Fragment ؒͰσʔλΛܕ҆શʹ౉ͤΔ • Navigation editor Λ࢖͏ͱ GUI Ͱ؆୯ʹઃఆ͕Ͱ͖Δ

Slide 11

Slide 11 text

Navigation

Slide 12

Slide 12 text

Gradle - dependencies dependencies { def nav_version = "2.1.0-alpha06" implementation “androidx.navigation:navigation-fragment-ktx:nav_version" implementation “androidx.navigation:navigation-ui-ktx:nav_version” }

Slide 13

Slide 13 text

Gradle - dependencies buildscript { repositories { google() } dependencies { def nav_version = "2.1.0-alpha06" classpath “androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } } "// Kotlin apply plugin: "androidx.navigation.safeargs.kotlin"

Slide 14

Slide 14 text

Basic • ͜͜Ͱ͸ɺ؆୯ͳ Navigation editor ͷ࢖͍ํ΍ཁૉͷઆ໌Λͯ͠ ͍͖·͢ɻ

Slide 15

Slide 15 text

nav_graph.xml • res/navigation ͷ഑Լʹ Navigation ༻ ͷ xml Λ࡞Γɺ։͍ͯݟ͍ͯͩ͘͞ɻ
 ※ ͜͜Ͱ͸ nav_graph.xml ͱ͠·͢

Slide 16

Slide 16 text

nav_graph.xml Destinations panel

Slide 17

Slide 17 text

nav_graph.xml Graph Editor

Slide 18

Slide 18 text

nav_graph.xml Attributes

Slide 19

Slide 19 text

nav_graph.xml GUI ্Ͱ֤ը໘Λ௥Ճ͢Δ৔߹ʹ͸ɺ ͜ͷϘλϯ͔Β؆୯ʹ௥ՃͰ͖·͢ɻ

Slide 20

Slide 20 text

Demo: Navigation editor

Slide 21

Slide 21 text

nav_graph.xml Ͱར༻͢Δओͳλά • : ϧʔτཁૉɻىಈޙͷ։࢝ҐஔͳͲΛࢦఆ • : ֤ը໘ʢFragmentʣ • : ը໘ؒͷ֤ը໘ʢFragmentʣ • : ը໘Ͱड͚औΔҾ਺ • : σΟʔϓϦϯΫ • : DialogFragmentΛར༻͍ͯ͠Δ৔߹ͷը໘ • : Nested graphsɻάϥϑΛ෼ׂͯ͠੔ཧɾ࠶ར༻

Slide 22

Slide 22 text

nav_graph.xml " " " ֤λάΛઆ໌͍͖ͯ͠·͢ɻ

Slide 23

Slide 23 text

" ϧʔτཁૉʹͳΓ·͢ ͜ͷதʹ֤λάΛهࡌ͖ͯ͠·͢ɻ ޙड़͠·͕͢ɺىಈ࣌ͷॳظը໘ͷઃఆ΋͜͜ʹ͠·͢

Slide 24

Slide 24 text

" " ͜ͷλάͰ͸ Fragment ͷ֘౰Ϋϥεͱࢦఆ͠·͢ ·ͨɺޙड़͠·͕͢ , < argument/>, ͳͲ͸͜ͷ഑Լʹهࡌ͠·͢ɻ

Slide 25

Slide 25 text

" " ͜ͷ HomeFragment Λىಈ࣌ͷॳظը໘ͱ͢Δ৔߹͸ ͷ startDestination ʹࢦఆ͠·͢

Slide 26

Slide 26 text

GUI ্ͩͱɺ͜ͷҰͭʹ֘౰͠·͢ɻ

Slide 27

Slide 27 text

" " Ҿ਺ʹ͸ integer, float, long, boolean, string, reference, parcelable, serializable, enum ͷར༻͕Մೳ ར༻Ͱ͖Δܕɿhttps://developer.android.com/guide/navigation/navigation-pass-data#supported_argument_types

Slide 28

Slide 28 text

" " ͰॏཁʹͳΔͷ͕ id ͱ destination Ͱ͢ɻ id ͸ setOnClickListener ͳͲͰࢦఆ͢Δͱ͖ʹ࢖ ͍ɺdestination ͸ભҠઌͷ Fragment Λࢦఆ͠·͢

Slide 29

Slide 29 text

with anim ભҠ࣌ͷΞχϝʔγϣϯΛ௥Ճ͢Δ৔߹͸ɺ͜ͷΑ͏ʹͳΓ·͢ɻ

Slide 30

Slide 30 text

" σΟʔϓϦϯΫͰىಈ͍ͨ͠ Fragment ͷ഑Լ ʹىಈ͢Δ URIɺҾ਺ Λઃఆ͠·͢ɻ

Slide 31

Slide 31 text

- AndroidManifest.xml AndroidManifest ʹ Ͱ͸ͳ͘ Λ௥Ճ͢Δ͜ͱͰɺ͜͜ͷઃఆ ͸׬݁ͳΓ·͢ " " " "

Slide 32

Slide 32 text

" " ࢖͍ํ͸ ͱಉ͡Ͱ DialogFragment Λ Navigation Ͱར༻͍ͨ͠৔߹͸ͪ͜ΒΛ࢖͍·͢ɻ

Slide 33

Slide 33 text

NavHostFragment activity_main.xml ʹը໘੍ޚ͢Δ NavHostFragment Λઃఆ͠ɺ࡞੒ͨ͠ nav_graph.xml Λࢦఆ͢Δඞཁ͕͋Γ·͢ɻ

Slide 34

Slide 34 text

Advanced • Nested navigation graph • BottomNavigation • FragmentNavigator

Slide 35

Slide 35 text

Nested navigation graphs ͜ΕʹΑΓ͜Ε·Ͱ̍ͭͷ xml Ͱ ̍ͭͷ Activity ͕ભҠ͢Δ Fragment ͷઃఆ͕શͯ෼ׂɾ࠶ར༻Ͱ͖ΔΑ͏ʹͳΓ·͠ ͨɻ

Slide 36

Slide 36 text

Nested navigation graphs ଞͷάϥϑΛ ͢Δ͜ͱ͕Ͱ͖·͢ɻ

Slide 37

Slide 37 text

Z BottomNavigation "// MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navController = findNavController(R.id.nav_host_fragment) "// ActionBarͷλΠτϧͳͲΛ੍ޚ setupActionBarWithNavController( navController, AppBarConfiguration( setOf( R.id.fragment_home, R.id.fragment_dashboard ) ) ) "// ભҠΛ੍ޚ nav_view.setupWithNavController(navController) } ͜Ε͚ͩͰ BottomNavigation ͱͷ࿈ܞՄೳ Ͱࢦఆ͢Δ Id ͱҰகͤ͞Δ —> "

Slide 38

Slide 38 text

FragmentNavigator nav_graph.xml ͰΧελϜλάΛ࡞ΕΔ Α͏ʹͳΓ·͢ɻ "

Slide 39

Slide 39 text

FragmentNavigator ͜ΕʹΑΓ͜Ε·Ͱ̍ͭͷ xml Ͱ ̍ͭͷ Activity ͕ભҠ͢Δ Fragment ͷઃఆ͕શͯ෼ׂɾ࠶ར༻Ͱ͖ΔΑ͏ʹͳΓ·͠ ͨɻ @Navigator.Name("fragment_with_anim") class DefaultNavigator( context: Context, manager: FragmentManager, containerId: Int ) : FragmentNavigator(context, manager, containerId) { override fun navigate( destination: Destination, args: Bundle?, navOptions: NavOptions?, navigatorExtras: Navigator.Extras? ): NavDestination? { return super.navigate(destination, args, exOp, navigatorExtras) } }

Slide 40

Slide 40 text

FragmentNavigator MainActivity Ͱ ࡞੒ͨ͠ FragmentNavigator Λ NavController ʹ௥Ճ͠·͢ɻ ※ άϥϑ͕ઃఆ͞ΕΔલʹɺߦΘͳ͍ͱ͍͚·ͤΜɻ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) findNavController(R.id.nav_host_fragment).apply { navigatorProvider += DefaultNavigator( this@MainActivity, nav_host_fragment.childFragmentManager, R.id.nav_host_fragment ) setGraph(R.navigation.nav_graph) } }

Slide 41

Slide 41 text

՝୊

Slide 42

Slide 42 text

՝୊0 • ՝୊Ͱ࢖͏ίʔυͪ͜ΒͰ͢ɻ
 https://github.com/wasabeef/NavPlayground
 
 git clone [email protected]:wasabeef/NavPlayground.git
 
 
 Ϟδϡʔϧߏ੒
 app-step0 = ࡞ۀ༻Ϟδϡʔϧ

Slide 43

Slide 43 text

՝୊ • ՝୊1
 Navigation Խͯ͠ɺHome -> DashboardΛભҠͤ͞·͠ΐ͏ɻ
 ՝୊1.5 ભҠ࣌ͷΞχϝʔγϣϯΛ௥Ճͯ͠Έ·͠ΐ͏ɻ • ՝୊2
 Λ࢖ͬͯ Dashboard ʹ id:String, name:String, location:String Λ౉ͯ͠Έ·͠ΐ͏ɻ • ՝୊3
 Λ࢖ͬͯ Dashboard ͕͜ͷURLͰىಈͰ͖ΔΑ͏ʹ͠·͠ΐ͏ɻ
 https://sample.com/dashboard/{id}/{name}
 
 ىಈςετɿ
 adb shell am start -W -a android.intent.action.VIEW \
 -d "https://sample.com/dashboard/xxxxx/yyyyy"

Slide 44

Slide 44 text

՝୊ʢ͕࣌ؒ͋Ε͹ʣ • ՝୊4
 Λ࢖ͬͯάϥϑΛ෼ׂͯ͠Έ·͠ΐ͏ɻ
 • ՝୊5
 BottomNavigation ͷ࣮૷Λ Navigation Λ࢖ͬͯΈ·͠ΐ͏ɻ

Slide 45

Slide 45 text

౴͑ • ՝୊1 -> app-step1-action, app-step1-action-with-anim • ՝୊2 -> app-step2-args • ՝୊3 -> app-step3-deeplink • ՝୊4 -> app-step4-nested • ՝୊5 -> app-step5-bottom-nav • ՝୊6 -> app-step6-navigator

Slide 46

Slide 46 text

ࢀߟࢿྉ • Android Navigation Architecture Component
 https://medium.com/@Alex.v/android-navigation-architecture- component-25b5a7aab8aa • Get started with the Navigation component
 https://developer.android.com/guide/navigation/navigation-getting- started •