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

AndroidX Navigationを導入した話 / Introducing AndroidX Navigation

AndroidX Navigationを導入した話 / Introducing AndroidX Navigation

Masatoshi Kubode

February 28, 2019
Tweet

More Decks by Masatoshi Kubode

Other Decks in Programming

Transcript

  1. ©2019 Wantedly, Inc.
    "OESPJE9/BWJHBUJPO
    Λಋೖͨ͠࿩
    QPUBUPUJQT
    ٱอग़խढ़
    2019/02/28

    View full-size slide

  2. ©2019 Wantedly, Inc.
    ٱอग़խढ़
    8BOUFEMZ
    ओʹ8BOUFEMZ7JTJUͷ"OESPJEΞϓϦΛ։ൃ͍ͯ͠·͢ɻ
    ઈࢍϦχϡʔΞϧதɻ
    QPUBUPUJQT͸ճ࿈ଓग़৔தɻ
    ,PUMJO
    1PLFNPO(P
    ࣗݾ঺հ
    5XJUUFS
    !TXJ[@BSE
    (JU)VC
    !LVCPEF

    View full-size slide

  3. ©2019 Wantedly, Inc.
    "OESPJE9/BWJHBUJPOͱ͸
    ͳͥಋೖͨ͠ͷ͔
    ࣮૷
    ൃੜͨ͠໰୊఺
    ·ͱΊ
    ࿩͢͜ͱ

    View full-size slide

  4. ©2019 Wantedly, Inc.
    "OESPJE9/BWJHBUJPOͱ͸

    View full-size slide

  5. ©2019 Wantedly, Inc.
    w "OESPJE9ͷͻͱͭ
    w چ
    /BWJHBUJPO$PNQPOFOUT
    w ͦΖͦΖTUBCMF ݱ࣌఺Ͱ3$

    w TUBCMFʹͳΔͱਖ਼ࣜʹ"OESPJE9ʹͳͬͯഁյత
    มߋ͕͋Δ͔΋
    AndroidX Navigationͱ͸

    View full-size slide

  6. ©2019 Wantedly, Inc.
    w (6*ͰͷભҠਤ
    w 9.-Ͱ΋هड़Մ
    ػೳ

    View full-size slide

  7. ©2019 Wantedly, Inc.
    w ը໘Ҿ਺
    w 4BGF"SHTͰͷܕ҆શͳ

    'SBHNFOU"SHTͷੜ੒
    w %FFQ-JOLରԠ
    w 5SBOTJUJPO"OJNBUJPO
    w 4IBSFE&MFNFOU
    ػೳ

    View full-size slide

  8. ©2019 Wantedly, Inc.
    w BSHVNFOUͷΫϥεΛੜ੒͢Δ(SBEMF1MVHJO
    w ܕ҆શʹͳΔ
    w 'SBHNFOUOFX*OTUBODFͱ͔࡞Βͳͯ͘Α͍
    w /BWJHBUJPO࢖Θͣ͜Ε͚ͩΛಋೖ͢Δέʔε΋
    SafeArgs
    apply plugin: "androidx.navigation.safeargs.kotlin"

    View full-size slide

  9. ©2019 Wantedly, Inc.
    ͳͥಋೖͨ͠ͷ͔

    View full-size slide

  10. ©2019 Wantedly, Inc.
    w ΦϯϘʔσΟϯάΛϦχϡʔΞϧ
    w "DUJWJUZͰϑϩʔΛ؅ཧ
    w ෳ਺ͷը໘͸'SBHNFOUͰ࡞Δ
    ͳͥಋೖͨ͠ͷ͔

    View full-size slide

  11. ©2019 Wantedly, Inc.
    OnboardingActivity
    ϑϩʔͷߏ੒
    InitialFragment WelcomeFragment SelectOccupationFragment
    MainActivity

    View full-size slide

  12. ©2019 Wantedly, Inc.
    w ৚݅෼ذ͕

    ͋Δ
    w ύϥϝʔλʔ

    ͷड͚౉͠
    w جຊతʹ

    Ұํ௨ߦ
    ࣮ࡍͷϑϩʔ

    View full-size slide

  13. ©2019 Wantedly, Inc.
    w ΞϓϦશମͷϦχϡʔΞϧΛਐΊ͍ͯΔ
    w .VMUJNPEVMFԽ͍ͯ͠Δ
    w 'FBUVSFؒͷը໘ભҠΛͲ͏͢Δ͔
    w ˠ%SPJE,BJHJͷ࣮૷͕ࢀߟʹͳΓͦ͏
    w ˠ/BWJHBUJPOΛࢼ͍ͨ͠
    ଞͷಋೖཧ༝

    View full-size slide

  14. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2019 Wantedly, Inc.
    /BWJHBUJPO΍Δͧ

    View full-size slide

  15. ©2019 Wantedly, Inc.
    ࣮૷

    View full-size slide

  16. ©2019 Wantedly, Inc.
    ۭͷ'SBHNFOUΫϥεΛ͢΂ͯ࡞Δ
    SFTOBWJHBUJPOOBWJHBUJPOYNMΛ࡞Δ
    (6*Ͱը໘Λ഑ஔ
    "DUJWJUZʹ/BW)PTU'SBHNFOUΛ഑ஔ
    'SBHNFOUͰ/BW$POUSPMMFSOBWJHBUFΛॻ͘
    ࣮૷ͷྲྀΕ

    View full-size slide

  17. ©2019 Wantedly, Inc.
    ը໘ͷ഑ஔ

    View full-size slide

  18. ©2019 Wantedly, Inc.
    ભҠΛͭͳ͛Δ

    View full-size slide

  19. ©2019 Wantedly, Inc.
    NavHostFragmentͷ഑ஔ
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/onboarding_navigation"
    />

    View full-size slide

  20. ©2019 Wantedly, Inc.
    NavController.navigateͰભҠ
    // InitialFragment
    override fun onSignUpSuccessful(userName: String) {
    findNavController() // KTXͷ֦ு
    .navigate(
    // safeargsͰੜ੒͞Εͨίʔυ
    InitialFragmentDirections
    .actionInitialFragmentToEmailWelcomeFragment(userName)
    )
    }

    View full-size slide

  21. ©2019 Wantedly, Inc.
    ൃੜͨ͠໰୊఺

    View full-size slide

  22. ©2019 Wantedly, Inc.
    w όοΫΩʔͰ໭Εͯ͠·͏
    w ը໘͝ͱʹঢ়ଶΛ͍࣋ͬͯΔͷͰ໭ΔͱόάΔ
    w 1PQ#FIBWJPSͰ໭Γઌ͸ࢦఆͰ͖Δ͕ɾɾɾ
    w ໭Βͤͳ͘͢Δ͜ͱ͸/BWJHBUJPOͰ͸Ͱ͖ͳ͍
    ໰୊఺1

    View full-size slide

  23. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2019 Wantedly, Inc.
    "OESPJE9"DUJWJUZ

    View full-size slide

  24. ©2019 Wantedly, Inc.
    w BEE0O#BDL1SFTTFE$BMMCBDL͕௥Ճ
    w όοΫΩʔ੍ޚΛ'SBHNFOUͰ࣮૷Ͱ͖Δ
    AndroidX Activity
    // WelcomeFragment
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    // Disable back key
    requireActivity().addOnBackPressedCallback(
    viewLifecycleOwner,
    OnBackPressedCallback { true }
    )
    }

    View full-size slide

  25. ©2019 Wantedly, Inc.
    ൃੜͨ͠໰୊఺

    View full-size slide

  26. ©2019 Wantedly, Inc.
    w pUT4ZTUFN8JOEPXT

    ͕ޮ͔ͳ͍
    w /BW)PTU'SBHNFOU͕

    'SBNF-BZPVUΛ࢖͍ͬͯΔ
    w 'SBNF-BZPVU͸

    pUT4ZTUFN8JOEPXT͕ޮ͔ͳ͍
    ໰୊఺2

    View full-size slide

  27. ©2019 Wantedly, Inc.
    ໰୊఺2ͷղܾࡦ
    class FitsSystemWindowsNavHostFragment : NavHostFragment() {
    override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
    ): View? {
    // CoordinatorLayout͸fitsSystemWindowsʹରԠ͍ͯ͠Δ
    return CoordinatorLayout(inflater.context).apply {
    fitsSystemWindows = true
    id = this.id // ͜Ε͸NavHostFragmentͷ࣮૷ͱಉ͡
    }
    }
    }

    View full-size slide

  28. ©2019 Wantedly, Inc.
    ·ͱΊ

    View full-size slide

  29. ©2019 Wantedly, Inc.
    w ࢼݧతʹಋೖ͚ͨ͠Ͳ࢖͍উख͕Α͔ͬͨ
    w 'SBHNFOU5SBOTBDUJPO
    w ଍Γͳ͍ͱ͜Ζ΋͋Δ͕ଞͷ"OESPJE9Ͱิ͑ͨ
    w কདྷతʹ͸શମΛ/BWJHBUJPOʹஔ͖׵͍͑ͨ
    ·ͱΊ

    View full-size slide

  30. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2019 Wantedly, Inc.
    5IBOLZPV

    View full-size slide