android - 如何将值传递回导航架构组件中的片段?
问题描述
假设我在片段 A 中有一个类的 obj 并将其传递给片段 B。当单击向上按钮(后退按钮)时,我希望将相同的对象传回。
我可以知道何时单击向上按钮(后退按钮)并了解片段名称。但是发回参数不起作用。它显示学生 val 丢失并且没有 defaultValue。当我给出默认值时,它会给出错误,为 public open fun fragmentBTofragnentA 提供的参数过多
这是下面的代码。
覆盖乐趣 onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.myNavHostFragment)
Log.d("Up button", "Main activity")
val id = navController.currentDestination!!.id
Log.d("Now id is: ", "${id.toString()}")
val label = navController.currentDestination!!.label
Log.d("Now id is: ", "${label.toString()}")
if (label == "FragmentB") {
navController.navigate(FragmentBDirections.actionFragmentBToFragmentA(
obj
)
return false
}
return navController.navigateUp()
导航文件
<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"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.example.fragmentdatapassings.FragmentA"
android:label="FragmentA">
<action
android:id="@+id/action_fragmentA_to_fragnentB"
app:destination="@+id/fragmentB" />
<argument android:name="student"
app:argType="com.example.StudentWork.Student" />
</fragment>
<fragment
android:id="@+id/fragmentB"
android:name="com.example.fragmentdatapassings.FragmentB"
android:label="FragmentB">
<action
android:id="@+id/action_fragmentB_to_fragnentA"
app:destination="@+id/fragmentA" />
<argument android:name="student"
app:argType="com.example.StudentWork.Student" />
</fragment>
任何帮助,将不胜感激。提前致谢。
解决方案
您可以如下添加默认值和可为空的属性,并检查错误是否已解决。您还可以查看此示例。
<argument android:name="student"
app:argType="com.example.StudentWork.Student"
android:defaultValue="@null"
app:nullable="true" />
推荐阅读
- c++ - 比赛条件是如何引起的 || 在 std::atomic? 中使用运算符而不是 &&?
- php - Setcookie() 在 Safari 中不起作用,但在 chrome 中可以正常工作
- kubernetes - Ubuntu 升级到 19.10 后 Minikube 将无法工作
- javascript - 在加载 webapp 之前获取数据、编辑 MongoDB 集合?异步,承诺?
- java-8 - 出现错误:在 ANT 构建期间包 org.docx4j 不存在
- sql - Power BI:关系具有基数多对多问题
- inheritance - 适合案例的设计模式
- cypress - 无法将鼠标悬停在被另一个元素覆盖的菜单选项上
- html - 如何在 Html String 中给出相对图像路径?
- python-3.x - 使用 python3 代码单击 OK WinForm 按钮