Ism*_*ail 12 android-navigation android-architecture-navigation
如何使用导航架构组件 safeargs 将参数传递给对话框片段?下面是我当前的实现
开始片段
val navController = findNavController()
val action =
QuestionListFragmentDirections.actionQuestionListFragmentToCustomDialogFragment(args.templateFlag)
navController.navigate(
action
)
Run Code Online (Sandbox Code Playgroud)
目的地片段
args.templateFlage //supposed to return a boolean value
//but throws java.lang.IllegalStateException: Fragment QuestionTypeDialogFragment{e8be5e1}
(47b305ea-35b2-49e0-b378-d31a08ba9a41) QuestionTypeDialogFragment} has null arguments
Run Code Online (Sandbox Code Playgroud)
小智 20
可能是您在片段获得之前使用了 args 。例如我在代码中更改了它:
private val args by navArgs<RadioDetailFragmentArgs>()
private val stations = args.stations.toList()
Run Code Online (Sandbox Code Playgroud)
和
private val args by navArgs<RadioDetailFragmentArgs>()
private val stations by lazy {
args.stations.toList()
}
Run Code Online (Sandbox Code Playgroud)
它有效
Mar*_* RS -2
查看有关在目的地之间传递数据的说明:
在导航图中添加对话框片段的条目。
<dialog
android:id="@+id/questionListFragment"
android:name="com.path.to.my.QuestionListFragment"
android:label="Question List"
tools:layout="@layout/layout_of_fragment">
<argument
android:name="ArgName"
android:defaultValue="@null"
app:argType="boolean"
app:nullable="true" />
</dialog>
Run Code Online (Sandbox Code Playgroud)
然后,在可以启动对此对话框片段的导航的片段的声明中,您将需要添加一个操作:
<action
android:id="@+id/actionQuestionListFragmentToCustomDialogFragment"
app:destination="@id/chooseVideo"
/>
Run Code Online (Sandbox Code Playgroud)
最后,在启动导航的片段中,您可以调用函数导航:
val directions = QuestionListFragmentDirections.actionQuestionListFragmentToCustomDialogFragment(args.templateFlag)
findNavigationController().navigate(directions)
Run Code Online (Sandbox Code Playgroud)
请注意,我们设置的 QuestionListFragment 参数android:defaultValue="@null"意味着布尔值可以为 null。如果不是这种情况,那么您将需要将其删除。
| 归档时间: |
|
| 查看次数: |
8555 次 |
| 最近记录: |