$30 off During Our Annual Pro Sale. View Details »

Navigationのバックキーハンドリング.pdf

 Navigationのバックキーハンドリング.pdf

kosuke matsumura

August 02, 2018
Tweet

More Decks by kosuke matsumura

Other Decks in Programming

Transcript

  1. #BDLLFZIBOEMJOHXJUI/BWJHBUJPO TIJCVZBBQL  দଜߤ༟

  2. "CPVU.F w দଜߤ༟ ,PTVLF.BUTVNVSB  w /"7*5*.&+"1"/$P -UE
 "OESPJEJ04%FWFMPQFS w

    ,BXBTBLJ'SPOUBMF4VQQPSUFS w 5XJUUFS
 NLPTVLF!LPTVLF@NUN
  3. 5PEBZ`TUIFNF w )PXUPIBOEMFCBDLLFZQSFTTXJUI/BWJHBUJPO w ฤूը໘ͳͲͰόοΫΩʔΛԡԼͨ͠ࡍʹɺʮฤूΛഁغͯ͠΋ΑΖ͍͠ Ͱ͔͢ʁʯμΠΞϩάΛग़͍ͨ͠ͱ͖ͳͲΛ૝ఆ͍ͯ͠·͢
 ˞࢓༷ͷྑ͠ѱ͠͸͜͜Ͱ͸͠·ͤΜ

  4. 8IBU`T/BWJHBUJPO w 0OFDPNQPOFOUTPG"OESPJE+FUQBDL w /BWJHBUJPONBLFT'SBHNFOUUSBOTJUJPOFBTJFS

  5. /BWJHBUJPO(SBQI ʮ໭Δʯ౳ͷը໘ભҠ͸ఆٛͰ͖Δ͕ɺ
 ʮόοΫΩʔʯͷΠϕϯτൃੜ࣌ͷॲཧ͸ॻ͚ͳ͍

  6. ࣮૷ํ๏

  7. "DUJWJUZ override fun onBackPressed() { val navHost = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment)

    as NavHostFragment val target = navHost.childFragmentManager .findFragmentById(R.id.my_nav_host_fragment) if (target is OnBackPressHandler) { if (target.onBackPressed()) { return } } super.onBackPressed() } όοΫΩʔͷ࠷ॳͷΠϕϯτड͚औΓޱͰ͋Δ"DUJWJUZͰ
 ΠϕϯτΛड͚औΓ·͢
  8. "DUJWJUZ override fun onBackPressed() { val navHost = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment)

    as NavHostFragment val target = navHost.childFragmentManager .findFragmentById(R.id.my_nav_host_fragment) if (target is OnBackPressHandler) { if (target.onBackPressed()) { return } } super.onBackPressed() } /BW)PTU'SBHNFOUΛऔಘ͠·͢
  9. /BW)PTU'SBHNFOU /BWJHBUJPOͰը໘ભҠΛߦ͏৔߹ͷ3PPUʹ૬౰͢Δ'SBHNFOUɻ
 /BWJHBUJPO͸ɺ͜ͷ/BW)PTU'SBHNFOUͷ্ͰDIJME'SBHNFOUͱͯ͠ը໘ભ ҠΛߦ͍·͢ /BW)PTU'SBHNFOU 'SBHNFOU" 'SBHNFOU# 'SBHNFOU$ DIJMEGSBHNFOU OBWJHBUF

    OBWJHBUF
  10. /BW)PTU'SBHNFOU /BWJHBUJPOͰը໘ભҠΛߦ͏৔߹ͷ3PPUʹ૬౰͢Δ'SBHNFOUɻ
 /BWJHBUJPO͸ɺ͜ͷ/BW)PTU'SBHNFOUͷ্ͰDIJME'SBHNFOUͱͯ͠ը໘ભ ҠΛߦ͍·͢ <LinearLayout …> <android.support.v7.widget.Toolbar …/> <fragment android:id="@+id/my_nav_host_fragment"

    android:name="androidx.navigation.fragment.NavHostFragment" app:navGraph="@navigation/mobile_navigation" app:defaultNavHost="true" …/> </LinearLayout> /BW)PTU'SBHNFOU
  11. /BW)PTU'SBHNFOU /BWJHBUJPOͰը໘ભҠΛߦ͏৔߹ͷ3PPUʹ૬౰͢Δ'SBHNFOUɻ
 /BWJHBUJPO͸ɺ͜ͷ/BW)PTU'SBHNFOUͷ্ͰDIJME'SBHNFOUͱͯ͠ը໘ભ ҠΛߦ͍·͢ <LinearLayout …> <android.support.v7.widget.Toolbar …/> <fragment android:id="@+id/my_nav_host_fragment"

    android:name="androidx.navigation.fragment.NavHostFragment" app:navGraph="@navigation/mobile_navigation" app:defaultNavHost="true" …/> </LinearLayout> "DUJWJUZͰࢀর͍ͯͨ͠JE
  12. "DUJWJUZ override fun onBackPressed() { val navHost = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment)

    as NavHostFragment val target = navHost.childFragmentManager .findFragmentById(R.id.my_nav_host_fragment) if (target is OnBackPressHandler) { if (target.onBackPressed()) { return } } super.onBackPressed() } /BW)PTU'SBHNFOUΛऔಘ͠·͢
  13. "DUJWJUZ override fun onBackPressed() { val navHost = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment)

    as NavHostFragment val target = navHost.childFragmentManager .findFragmentById(R.id.my_nav_host_fragment) if (target is OnBackPressHandler) { if (target.onBackPressed()) { return } } super.onBackPressed() } /BW)PTU'SBHNFOU͔Βݱࡏදࣔதͷը໘ 'SBHNFOU Λऔಘ͠·͢
  14. /BW)PTU'SBHNFOU`TDIJMEGSBHNFOUT /BW)PTU'SBHNFOUʹ͸ͭͷDIJME'SBHNFOU͕BUUBDI͞Ε͍ͯ·͢
 ͻͱͭ͸ݱࡏදࣔதͷ'SBHNFOUɺ΋͏ͻͱͭ͸4UBUF'SBHNFOUͰ͢ // FragmentNavigation.java public void navigate(Destination dest, Bundle

    args, NavOptions option){ final Fragment frag = dest.createFragment(args); final FragmentTransaction ft = mFragmentManager.beginTransaction(); (్தུ…) ft.replace(mContainerId, frag); final StateFragment oldState = getState(); if (oldState != null) { ft.remove(oldState); } final @IdRes int destId = dest.getId(); final StateFragment newState = new StateFragment(); newState.mCurrentDestId = destId; ft.add(newState, StateFragment.FRAGMENT_TAG); ݱࡏදࣔதͷ'SBHNFOU N$POUBJOFS*E͸/BW)PTU'SBHNFOUͱಉ͡*% 4UBUF'SBHNFOU
  15. 4UBUF'SBHNFOU @RestrictTo(RestrictTo.Scope.LIBRARY) public static class StateFragment extends Fragment { static

    final String FRAGMENT_TAG = “…FragmentNavigator.StateFragment”; private static final String KEY_CURRENT_DEST_ID = "currentDestId"; int mCurrentDestId; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { mCurrentDestId = savedInstanceState.getInt(KEY_CURRENT_DEST_ID); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(KEY_CURRENT_DEST_ID, mCurrentDestId); } } 6*Λ࣋ͨͳ͍'SBHNFOU ʮ໭Δʯ࣌ͷ*%Λอ͍࣋ͯ͠Δ͚ͩ
  16. "DUJWJUZ override fun onBackPressed() { val navHost = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment)

    as NavHostFragment val target = navHost.childFragmentManager .findFragmentById(R.id.my_nav_host_fragment) if (target is OnBackPressHandler) { if (target.onBackPressed()) { return } } super.onBackPressed() } /BW)PTU'SBHNFOU͔Βݱࡏදࣔதͷը໘ 'SBHNFOU Λऔಘ͠·͢
  17. "DUJWJUZ override fun onBackPressed() { val navHost = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment)

    as NavHostFragment val target = navHost.childFragmentManager .findFragmentById(R.id.my_nav_host_fragment) if (target is OnBackPressHandler) { if (target.onBackPressed()) { return } } super.onBackPressed() } ͋ͱ͸'SBHNFOU͕0O#BDL1SFTT)BOEMFSΛ࣮૷͍ͯͨ͠Β
 PO#BDL1SFTTFE ΛݺΜͰ͋͛Δ͚ͩͰ͢
  18. 0O#BDL1SFTT)BOEMFS όοΫΩʔϋϯυϦϯά͍ͨ͠'SBHNFOUͰ͜ΕΛ࣮૷͠ɺ
 USVFΛฦ٫͠·͢ interface OnBackPressHandler { fun onBackPressed(): Boolean }

  19. #BDL1SFTTΛड͚औΔ'SBHNFOU override fun onBackPressed(): Boolean { AlertDialog.Builder(requireContext()) .setMessage("ݱࡏमਖ਼தͷ಺༰Λഁغͯ͠લͷը໘ʹ໭ͬͯ΋ΑΖ͍͠Ͱ͔͢ʁ") .setPositiveButton("ઃఆΛഁغ͢Δ") {

    _, _ -> Navigation.findNavController(requireActivity(), 
 R.id.my_nav_host_fragment).popBackStack() } .setNegativeButton("ฤूΛଓ͚Δ") { _, _ -> } .show() return true } ࣮ࡍʹCBDL1SFTTͱಉ౳ͷॲཧΛߦ͏ʹ͸
 /BW$POUSPMMFS͔ΒQPQ#BDL4UBDL Λ࣮ߦ͠·͢
  20. 5IBOLZPVGPSZPVSBUUFOUJPO

  21. 3FGFSFODFT w 2JJUB/BWJHBUJPOͰόοΫΩʔΛϋϯυϦϯά͢Δ
 IUUQTRJJUBDPNNLPTVLFJUFNTGEBEBBEF w "OESPJE+FUQBDLNBOBHF6*OBWJHBUJPOXJUI/BWJHBUJPO$POUSPMMFS
 IUUQTXXXZPVUVCFDPNXBUDI W($9U$KUH w /BWJHBUJPO$PEFMBC


    IUUQTDPEFMBCTEFWFMPQFSTHPPHMFDPNDPEFMBCTBOESPJEOBWJHBUJPO JOEFYIUNM JOEFY''JP w IUUQTEFWFMPQFSBOESPJEDPNUPQJDMJCSBSJFTBSDIJUFDUVSFOBWJHBUJPO