$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. "CPVU.F w দଜߤ༟ ,PTVLF.BUTVNVSB  w /"7*5*.&+"1"/$P -UE
 "OESPJEJ04%FWFMPQFS w

    ,BXBTBLJ'SPOUBMF4VQQPSUFS w 5XJUUFS
 NLPTVLF!LPTVLF@NUN
  2. "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Ͱ
 ΠϕϯτΛड͚औΓ·͢
  3. "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Λऔಘ͠·͢
  4. /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
  5. "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Λऔಘ͠·͢
  6. "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 Λऔಘ͠·͢
  7. /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
  8. 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 ʮ໭Δʯ࣌ͷ*%Λอ͍࣋ͯ͠Δ͚ͩ
  9. "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 Λऔಘ͠·͢
  10. "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 ΛݺΜͰ͋͛Δ͚ͩͰ͢
  11. #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 Λ࣮ߦ͠·͢
  12. 3FGFSFODFT w 2JJUB/BWJHBUJPOͰόοΫΩʔΛϋϯυϦϯά͢Δ
 IUUQTRJJUBDPNNLPTVLFJUFNTGEBEBBEF w "OESPJE+FUQBDLNBOBHF6*OBWJHBUJPOXJUI/BWJHBUJPO$POUSPMMFS
 IUUQTXXXZPVUVCFDPNXBUDI W($9U$KUH w /BWJHBUJPO$PEFMBC


    IUUQTDPEFMBCTEFWFMPQFSTHPPHMFDPNDPEFMBCTBOESPJEOBWJHBUJPO JOEFYIUNM JOEFY''JP w IUUQTEFWFMPQFSBOESPJEDPNUPQJDMJCSBSJFTBSDIJUFDUVSFOBWJHBUJPO