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

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

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

kosuke matsumura

August 02, 2018
Tweet

More Decks by kosuke matsumura

Other Decks in Programming

Transcript

  1. #BDLLFZIBOEMJOHXJUI/BWJHBUJPO
    TIJCVZBBQL

    দଜߤ༟

    View full-size slide

  2. "CPVU.F
    w দଜߤ༟ ,PTVLF.BUTVNVSB

    w /"7*5*.&+"1"/$P -UE

    "OESPJEJ04%FWFMPQFS
    w ,BXBTBLJ'SPOUBMF4VQQPSUFS
    w 5XJUUFS

    NLPTVLF!LPTVLF@NUN

    View full-size slide

  3. 5PEBZ`TUIFNF
    w )PXUPIBOEMFCBDLLFZQSFTTXJUI/BWJHBUJPO
    w ฤूը໘ͳͲͰόοΫΩʔΛԡԼͨ͠ࡍʹɺʮฤूΛഁغͯ͠΋ΑΖ͍͠
    Ͱ͔͢ʁʯμΠΞϩάΛग़͍ͨ͠ͱ͖ͳͲΛ૝ఆ͍ͯ͠·͢

    ˞࢓༷ͷྑ͠ѱ͠͸͜͜Ͱ͸͠·ͤΜ

    View full-size slide

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

    View full-size slide

  5. /BWJHBUJPO(SBQI
    ʮ໭Δʯ౳ͷը໘ભҠ͸ఆٛͰ͖Δ͕ɺ

    ʮόοΫΩʔʯͷΠϕϯτൃੜ࣌ͷॲཧ͸ॻ͚ͳ͍

    View full-size slide

  6. ࣮૷ํ๏

    View full-size slide

  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Ͱ

    ΠϕϯτΛड͚औΓ·͢

    View full-size slide

  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Λऔಘ͠·͢

    View full-size slide

  9. /BW)PTU'SBHNFOU
    /BWJHBUJPOͰը໘ભҠΛߦ͏৔߹ͷ3PPUʹ૬౰͢Δ'SBHNFOUɻ

    /BWJHBUJPO͸ɺ͜ͷ/BW)PTU'SBHNFOUͷ্ͰDIJME'SBHNFOUͱͯ͠ը໘ભ
    ҠΛߦ͍·͢
    /BW)PTU'SBHNFOU
    'SBHNFOU" 'SBHNFOU# 'SBHNFOU$
    DIJMEGSBHNFOU
    OBWJHBUF OBWJHBUF

    View full-size slide

  10. /BW)PTU'SBHNFOU
    /BWJHBUJPOͰը໘ભҠΛߦ͏৔߹ͷ3PPUʹ૬౰͢Δ'SBHNFOUɻ

    /BWJHBUJPO͸ɺ͜ͷ/BW)PTU'SBHNFOUͷ্ͰDIJME'SBHNFOUͱͯ͠ը໘ભ
    ҠΛߦ͍·͢


    android:id="@+id/my_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/mobile_navigation"
    app:defaultNavHost="true"
    …/>

    /BW)PTU'SBHNFOU

    View full-size slide

  11. /BW)PTU'SBHNFOU
    /BWJHBUJPOͰը໘ભҠΛߦ͏৔߹ͷ3PPUʹ૬౰͢Δ'SBHNFOUɻ

    /BWJHBUJPO͸ɺ͜ͷ/BW)PTU'SBHNFOUͷ্ͰDIJME'SBHNFOUͱͯ͠ը໘ભ
    ҠΛߦ͍·͢


    android:id="@+id/my_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/mobile_navigation"
    app:defaultNavHost="true"
    …/>

    "DUJWJUZͰࢀর͍ͯͨ͠JE

    View full-size slide

  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Λऔಘ͠·͢

    View full-size slide

  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
    Λऔಘ͠·͢

    View full-size slide

  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

    View full-size slide

  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
    ʮ໭Δʯ࣌ͷ*%Λอ͍࣋ͯ͠Δ͚ͩ

    View full-size slide

  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
    Λऔಘ͠·͢

    View full-size slide

  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
    ΛݺΜͰ͋͛Δ͚ͩͰ͢

    View full-size slide

  18. 0O#BDL1SFTT)BOEMFS
    όοΫΩʔϋϯυϦϯά͍ͨ͠'SBHNFOUͰ͜ΕΛ࣮૷͠ɺ

    USVFΛฦ٫͠·͢
    interface OnBackPressHandler {
    fun onBackPressed(): Boolean
    }

    View full-size slide

  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
    Λ࣮ߦ͠·͢

    View full-size slide

  20. 5IBOLZPVGPSZPVSBUUFOUJPO

    View full-size slide

  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

    View full-size slide