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

Navigation Componentのデータ受け渡しのつまづき

Suyama
February 12, 2020

Navigation Componentのデータ受け渡しのつまづき

概要:Android Jetpack Navigationコンポーネントの複雑な画面遷移を実装方法
背景:プロダクトコードの一部にNavigationを導入

目的:NavigationでNestedGraphを用いた場合のデータ受け渡し方法

Suyama

February 12, 2020
Tweet

More Decks by Suyama

Other Decks in Programming

Transcript

  1. // DirectionsΫϥε(ࣗಈੜ੒) class FirstFragmentDirections private constructor() { private data class

    ActionFirstToSecond( val hogeName: String = "hoge" ) : NavDirections { override fun getActionId(): Int = R.id.action_first_to_second override fun getArguments(): Bundle { val result = Bundle() result.putString("hogeName", this.hogeName) return result } } companion object { fun actionFirstToSecond(hogeName: String = "hoge"): NavDirections = ActionFirstToSecond(hogeName) } } 10
  2. // ArgsΫϥε(ࣗಈੜ੒) data class SecondFragmentArgs(val hogeName: String = "hoge") :

    NavArgs { fun toBundle(): Bundle { val result = Bundle() result.putString("hogeName", this.hogeName) return result } companion object { @JvmStatic fun fromBundle(bundle: Bundle): SecondFragmentArgs { bundle.setClassLoader(SecondFragmentArgs::class.java.classLoader) val __hogeName : String? if (bundle.containsKey("hogeName")) { __hogeName = bundle.getString("hogeName") if (__hogeName == null) { throw IllegalArgumentException("Argument \"hogeName\" is marked as non-null but was passed a null value.") } } else { __hogeName = "hoge" } return SecondFragmentArgs(__hogeName) } } } 11
  3. 16 // DirectionsΫϥε(ࣗಈੜ੒) class FirstFragmentDirections private constructor() { companion object

    { fun actionFirstToSecond(): NavDirections = ActionOnlyNavDirections(R.id.action_to_second) } }
  4. 19 // top_nav_graph.xml <fragment android:id="@+id/firstFragment" android:name="com.example.navigationsample.FirstFragment"> <action android:id="@+id/action_to_second" app:destination="@id/nested_nav_graph"> //

    ௥Ճ͢Δ͜ͱͰDirectionsͷҾ਺ͱͯ͠ೝࣝ <argument android:name="hogeName" app:argType="string" android:defaultValue="hoge" /> </action> </fragment> <include app:graph=“@navigation/nested_nav_graph“ />
  5. 20 // DirectionsΫϥε(ࣗಈੜ੒) class FirstFragmentDirections private constructor() { private data

    class ActionFirstToSecond( val hogeName: String = "hoge" ) : NavDirections { override fun getActionId(): Int = R.id.action_to_second override fun getArguments(): Bundle { val result = Bundle() result.putString("hogeName", this.hogeName) return result } } companion object { fun actionFirstToSecond(hogeName: String = "hoge"): NavDirections = ActionFirstToSecond(hogeName) } }