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

略解 The Navigation Architecture Component

yagi2
June 07, 2018

略解 The Navigation Architecture Component

2018/06/07 umeda.apk #4 LT
https://shibuya-apk.connpass.com/event/88738

Links
>> yagi2/Android-Navigation-Sample: The Navigation Architecture Component Sample App https://github.com/yagi2/Android-Navigation-Sample #NowBrowsing

【Android】Google IO 2018で新発表された navigation についての詳細レポート - DMM inside » https://inside.dmm.com/entry/2018/05/25/android-navigation

Navigation Component // Speaker Deck » https://speakerdeck.com/satsukies/navigation-component

yagi2

June 07, 2018
Tweet

More Decks by yagi2

Other Decks in Programming

Transcript

  1. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" android:name="com.yagi2.navigation.MainFragment" android:label="MainFragment" tools:layout="@layout/fragment_main" > </fragment> <fragment android:id="@+id/fragment_one" android:name="com.yagi2.navigation.FlowFragment" android:label="fragment_one" tools:layout="@layout/fragment_one" > </fragment> ……
  2. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id=“{֤ϑϥάϝϯτͷID}“ android:name="com.yagi2.navigation.MainFragment" android:label="MainFragment" tools:layout="@layout/fragment_main" > </fragment> ……
  3. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id=“{֤ϑϥάϝϯτͷID}“ android:name=“{ϑϥάϝϯτͷΫϥε}” android:label=“{Navigation EditorͰදࣔ͞ΕΔϥϕϧ}” tools:layout=“{Navigation EditorͰදࣔ͞ΕΔϨΠΞ΢τ}” > </fragment> ……
  4. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" … <action android:id=“@+id/action_main_to_one” app:destination="@id/fragment_one" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> ……
  5. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" … <action android:id=“@+id/action_main_to_one” app:destination="@id/fragment_one" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> ……
  6. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" … <action android:id=“{ભҠΞΫγϣϯͷID}” app:destination="@id/fragment_one" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> ……
  7. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" … <action android:id=“{ભҠΞΫγϣϯͷID}” app:destination=“{ભҠઌͷFragmentͷID}” app:enterAnim=“@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> ……
  8. XMLͷϦιʔεͰఆٛͰ͖Δ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" … <action android:id=“{ભҠΞΫγϣϯͷID}” app:destination=“{ભҠઌͷFragmentͷID}” app:enterAnim=“{ΞχϝʔγϣϯϦιʔεͷID}“ app:exitAnim="{ΞχϝʔγϣϯϦιʔεͷID}" app:popEnterAnim="{ΞχϝʔγϣϯϦιʔεͷID}" app:popExitAnim=“{ΞχϝʔγϣϯϦιʔεͷID}" /> </fragment> ……
  9. ͔͍͔ͭͨ <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"

    tools:context=".MainActivity" > <fragment android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:name="androidx.navigation.fragment.NavHostFragment" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true" /> </android.support.constraint.ConstraintLayout>
  10. ͔͍͔ͭͨ <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"

    tools:context=".MainActivity" > <fragment android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:name="androidx.navigation.fragment.NavHostFragment" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true" /> </android.support.constraint.ConstraintLayout>
  11. ͔͍͔ͭͨ <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"

    tools:context=".MainActivity" > <fragment android:id=“@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:name="androidx.navigation.fragment.NavHostFragment" app:navGraph=“{͖ͬ͞ఆٛͨ͠XMLͷNavigation Graph}” app:defaultNavHost="true" /> </android.support.constraint.ConstraintLayout>
  12. ͔͍͔ͭͨ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" android:name="com.yagi2.navigation.MainFragment" android:label="MainFragment" tools:layout="@layout/fragment_main" > </fragment> <fragment android:id="@+id/fragment_one" android:name="com.yagi2.navigation.FlowFragment" android:label="fragment_one" tools:layout="@layout/fragment_one" > </fragment> ……
  13. ͔͍͔ͭͨ <?xml version="1.0" encoding="utf-8"?> <navigation 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" app:startDestination="@id/fragment_main" >

    <fragment android:id="@+id/fragment_main" android:name="com.yagi2.navigation.MainFragment" android:label="MainFragment" tools:layout="@layout/fragment_main" > </fragment> <fragment android:id="@+id/fragment_one" android:name="com.yagi2.navigation.FlowFragment" android:label="fragment_one" tools:layout="@layout/fragment_one" > </fragment> ……
  14. ͔͍͔ͭͨ class MainFragment : Fragment() { override fun onCreateView(………): View?

    { super.onCreateView(inflater, container, savedInstanceState) return inflater.inflate(R.layout.fragment_main, container , false) } override fun onViewCreated(………) { super.onViewCreated(view, savedInstanceState) view.findViewById<Button>(R.id.next_button).setOnClickListener( Navigation.createNavigateOnClickListener(R.id.action_main_to_one) ) } }
  15. ͔͍͔ͭͨ class MainFragment : Fragment() { override fun onCreateView(………): View?

    { super.onCreateView(inflater, container, savedInstanceState) return inflater.inflate(R.layout.fragment_main, container , false) } override fun onViewCreated(………) { super.onViewCreated(view, savedInstanceState) view.findViewById<Button>(R.id.next_button).setOnClickListener( Navigation.createNavigateOnClickListener(R.id.action_main_to_one) ) } }
  16. SafeArgs class FlowFragment : Fragment() { override fun onCreateView(…): View?

    { super.onCreateView(…) val number = arguments?.getInt("number") return when (number) { 1 -> inflater.inflate(R.layout.fragment_one, container, false) 2 -> inflater.inflate(R.layout.fragment_two, container, false) 3 -> inflater.inflate(R.layout.fragment_three, container, false) else -> throw IllegalArgumentException() } } …
  17. SafeArgs class FlowFragment : Fragment() { override fun onCreateView(…): View?

    { super.onCreateView(…) val number = arguments?.getInt("number") return when (number) { 1 -> inflater.inflate(R.layout.fragment_one, container, false) 2 -> inflater.inflate(R.layout.fragment_two, container, false) 3 -> inflater.inflate(R.layout.fragment_three, container, false) else -> throw IllegalArgumentException() } } …
  18. SafeArgs class FlowFragment : Fragment() { override fun onCreateView(…): View?

    { super.onCreateView(…) val number = arguments?.getInt("number") return when (number) { 1 -> inflater.inflate(R.layout.fragment_one, container, false) 2 -> inflater.inflate(R.layout.fragment_two, container, false) 3 -> inflater.inflate(R.layout.fragment_three, container, false) else -> throw IllegalArgumentException() } } … Type Safe?
  19. SafeArgs public class FlowFragmentArgs { private int number = 3;

    private FlowFragmentArgs() { } public static FlowFragmentArgs fromBundle(Bundle bundle) { FlowFragmentArgs result = new FlowFragmentArgs(); if (bundle.containsKey("number")) { result.number = bundle.getInt("number"); } return result; } public int getNumber() { return number; } public Bundle toBundle() { Bundle __outBundle = new Bundle(); __outBundle.putInt("number", this.number); return __outBundle; }
  20. SafeArgs public class FlowFragmentArgs { private int number = 3;

    private FlowFragmentArgs() { } public static FlowFragmentArgs fromBundle(Bundle bundle) { FlowFragmentArgs result = new FlowFragmentArgs(); if (bundle.containsKey("number")) { result.number = bundle.getInt("number"); } return result; } public int getNumber() { return number; } public Bundle toBundle() { Bundle __outBundle = new Bundle(); __outBundle.putInt("number", this.number); return __outBundle; } Type Safe! ☺
  21. SafeArgs class FlowFragment : Fragment() { override fun onCreateView(…): View?

    { super.onCreateView(…) val number = arguments?.let { FlowFragmentArgs.fromBundle(it).number } return when (number) { 1 -> inflater.inflate(R.layout.fragment_one, container, false) 2 -> inflater.inflate(R.layout.fragment_two, container, false) 3 -> inflater.inflate(R.layout.fragment_three, container, false) else -> throw IllegalArgumentException() } } …
  22. SafeArgs class FlowFragment : Fragment() { override fun onCreateView(…): View?

    { super.onCreateView(…) val number = arguments?.let { FlowFragmentArgs.fromBundle(it).number } return when (number) { 1 -> inflater.inflate(R.layout.fragment_one, container, false) 2 -> inflater.inflate(R.layout.fragment_two, container, false) 3 -> inflater.inflate(R.layout.fragment_three, container, false) else -> throw IllegalArgumentException() } } …
  23. ৄղͷ঺հ ɾʲAndroidʳGoogle IO 2018Ͱ৽ൃද͞Εͨ navigation ʹ ͍ͭͯͷৄࡉϨϙʔτ - DMM inside

    ɹ https://inside.dmm.com/entry/2018/05/25/android- navigation ɾNavigation Component // Speaker Deck ɹhttps://speakerdeck.com/satsukies/navigation-component