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

Android Architecture Componentの Navigationで少しはまった話

Android Architecture Componentの Navigationで少しはまった話

bigbackboom

July 23, 2019
Tweet

More Decks by bigbackboom

Other Decks in Technology

Transcript

  1. Android
    Architecture Componentͷ
    NavigationͰগ͠͸·ͬͨ࿩

    View full-size slide

  2. ࣗݾ঺հ
    — ٠஑/bigbackboom
    — CBcloudגࣜձࣾɹ

    View full-size slide

  3. Navigationͱ͸ʁ
    — Google I/O 2018Ͱൃද͞ΕͨAndroid Architecture
    ComponentͷҰͭ
    — FragmentؒͷભҠΛϘτϜφϏͳͲͰߦ͏ͳͲෳࡶͳཁ݅
    ͷͨΊʹ։ൃ͞Εͨɻ
    — backstackͷ؅ཧ͕೉͍͠
    — fragmentࣗମͷlifecycle͕ෳࡶ
    — ݟͨ໨͸iOSͷstoryboardͳͲʹ͍͕ۙɺ࢖༻ൣғ͸͍ͩ

    View full-size slide

  4. ͳͥϘτϜφϏͰNavigationΛ࢖͏ඞཁ͕͋Δʁ
    — Α͘࢖ΘΕ͍ͯͨɺViewPager+BottomNavigationView͸
    ׂͱ࣮૷͕ෳࡶʢ࡞Δύʔπ͕ଟ͍ʣɻ
    — ViewPager͸ը໘ͷΩϟογϡॲཧ͕ಈ͘ͷͰɿ
    — ෳ਺ը໘͕ಉ࣌ʹಡΈࠐ·Εɺىಈ͕஗͘ͳΔ
    — Ωϟογϡ਺ʹΑͬͯ͸ɺը໘͕ഁغ͞Εબ୒࣌ʹҰ͔
    ΒಡΈࠐ·ΕΔɻ

    View full-size slide

  5. BottomNavigationView +
    NavigationΛ࣮૷
    — ͜ͷrepositoryΛϕʔεʹ࢖Θ
    ͤͯ΋Β͍·ͨ͠ɻ
    — ߏ଄͸ӈͷը૾ͷΑ͏ʹͳ͍ͬͯ
    ·͢ɻ

    View full-size slide

  6. λϒຖͷঢ়ଶҡ࣋
    — Կ΋͠ͳ͍ͱɺ
    DashboardFragmentͱ
    NotificationsFragment͸λϒ͕
    બ୒͞ΕΔͨͼʹॳظԽ͞ΕΔ
    — ͦΕΛආ͚ΔͨΊʹ͸ɺ
    BottomNavigationViewͱ
    Fragmentͷੜ੒Λ஥հ͢Δɺ
    FragmentNavigatorΛࣗ࡞͢Δ
    ඞཁ͕͋Δɻ

    View full-size slide

  7. — ࢀߟrepositoryͰ͸
    KeepStateNavigatorͱ͍͏΋ͷ
    Λ࡞͍ͬͯΔɻ
    — ·ͨɺBottomNavigationViewͱ
    Fragmentͷؔ܎Λఆٛͨ͠ɺ
    nav_graph.xmlͰ
    KeepStateNavigatorΛ࢖༻ͯ͠
    ͍Δ

    View full-size slide

  8. ͰɾɾɾԿʹ͸·ͬͨͷʁ
    — Activity͔ΒFragmentʹ஋Λ౉͢͜ͱ͕Ͱ͖ͳ͍ɾɾɾ
    — ௨ৗͰ͋Ε͹,ԼهͷΑ͏ʹ͢Ε͹஋Λϑϥάϝϯτʹ౉ͤ
    Δɻ
    val bundle = Bundle().apply {
    putString("NOTIFICATION_KEY", "bigbacbkboom is here")
    }
    NavController.navigate(item.itemId, bundle)

    View full-size slide

  9. ͕͔ͩ͠͠ɾɾɾ

    View full-size slide

  10. null ͩͱɾɾɾ

    View full-size slide

  11. ద౰ʹ౰ͨΓΛ͚ͭΔ

    View full-size slide

  12. KeepStateNavigator಺෦ͰσόοΨͰࢭΊͯΈΔ
    — Έ͚ͬͽΐΜ

    View full-size slide

  13. KeepStateNavigator.kt ίʔυΛमਖ਼
    ҎԼͷߦΛҰͭ௥Ճ͢Δ͚ͩɻ
    var fragment = manager.findFragmentByTag(tag)
    fragment?.arguments = args
    if (fragment == null) {
    val className = destination.className
    fragment = manager.fragmentFactory.instantiate(context.classLoader, className)
    transaction.add(containerId, fragment, tag)
    } else {
    transaction.attach(fragment)
    }

    View full-size slide

  14. मਖ਼಺༰͸ʁ
    — fragmentΛੜ੒͢Δͱ͖ʹɺBundleΛ౉ͯ͋͛͠ΔॲཧΛ
    ॻ͚͹͍͍ͱ͍͏ࢸۃ୯७ͳ΋ͷͰ͢ɻ
    — ͨͩɺਓͷίʔυΛૉ௚ʹίϐʔͯͨ͠ͷͰɺ΍͸Γ͜Ε
    ʹؾ͕ͭ͘ͷʹۤ࿑͠·ͨ͠ɻ

    View full-size slide

  15. ݁࿦
    — Navigation͸ViewPage+BottomNavigationViewrΑΓ΋
    ύʔπ਺͕গͳָ͘ɻ
    — ͲΜͳ͜ͱ΋ಉ͡Ͱ͕͢ɺ΍͸ΓͲΜͳ͜ͱ΋ͪΌΜͱཧ
    ղ͠ͳ͍ͱ௧͍໨ΛݟΔɻ

    View full-size slide

  16. We are Hiring!
    — Ұॹʹಇ͘஥ؒΛ୳͍ͯ͠·͢ɻ
    — αʔϏεࢤ޲ͷํ
    — ੵۃతʹఏҊͯ͠৽͍ٕ͠ज़ͷΤϯδχΞϦϯάΛߦ͍ͨ
    ͍ํ
    — ϚωʔδϝϯτΛ͍ͨ͠ํ
    https://www.wantedly.com/companies/cb-cloud

    View full-size slide