在导航组件中,从第一个片段向第二个片段发送参数时,默认值不会从导航图中获取。
这是我的代码:
导航图.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"
android:id="@+id/navigation_graph"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.example.navigationcomponent.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:enterAnim="@anim/nav_default_enter_anim" />
<argument
android:name="clickFrom"
app:argType="string"
android:defaultValue="From First Fragment" />
<argument
android:name="clickFragmentPosition"
app:argType="integer"
android:defaultValue="1" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.navigationcomponent.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" />
</navigation>
第一个片段:
class FirstFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val bundle = Bundle()
bundle.putBoolean("IsFirstFragment", true)
val navController = Navigation.findNavController(activity!!, R.id.my_nav_host_fragment)
btnNext.setOnClickListener {
navController.navigate(R.id.action_firstFragment_to_secondFragment, bundle)
}
}
}
第二片段:
class SecondFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val isFromFirstFragment = arguments?.getBoolean("IsFirstFragment", false)
Log.d(TAG, "$isFromFirstFragment")
Log.d(TAG, "${FirstFragmentArgs.fromBundle(arguments!!).clickFrom} ${FirstFragmentArgs.fromBundle(arguments!!).clickFragmentPosition}")
val navController = Navigation.findNavController(activity!!, R.id.my_nav_host_fragment)
btnBack.setOnClickListener {
navController.navigateUp()
}
navController.addOnDestinationChangedListener { controller, destination, arguments ->
Log.d("TAG", "${destination.label}");
}
}
companion object {
private const val TAG: String = "SecondFragment"
}
}
在这里,在第二个片段中获取默认值时,我得到空指针异常
Log.d(TAG, "${FirstFragmentArgs.fromBundle(arguments!!).clickFrom} ${FirstFragmentArgs.fromBundle(arguments!!).clickFragmentPosition}")
我的问题是,如何获取使用设置的参数值navigation_graph.xml
?重新构建项目时,导航图会自动生成 getter。是否有任何体系结构可以使用默认值绑定自动生成的设置器?