Share
Embed
Navigation Componentのデータ受け渡しのつまづき
概要:Android Jetpack Navigationコンポーネントの複雑な画面遷移を実装方法
背景:プロダクトコードの一部にNavigationを導入
目的:NavigationでNestedGraphを用いた場合のデータ受け渡し方法
Suyama
February 12, 2020
Transcript
22 slides
-
⾃⼰紹介 Name :Suyama Company :Studyplus Inc. Role :Android Engineer • potatotips参加2回⽬
• DroidKaigi2020に登壇予定 2 -
8 // nav_graph.xml <fragment android:id="@+id/secondFragment" android:name="com.example.navigationsample.SecondFragment"> // ࣗಈੜ <argument android:name="hogeName"
app:argType="string" android:defaultValue="hoge" /> </fragment> -
// 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 -
// 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 -
14 // 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" />
</fragment> <include app:graph=“@navigation/nested_nav_graph“ /> -
16 // DirectionsΫϥε(ࣗಈੜ) class FirstFragmentDirections private constructor() { companion object
{ fun actionFirstToSecond(): NavDirections = ActionOnlyNavDirections(R.id.action_to_second) } } -
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“ /> -
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) } }