android - 导航到片段时应用程序崩溃
问题描述
我正在为用户配置文件功能使用 3 个片段和 1 个活动。我使用导航移动到片段:
问题是当我从 profileFragment 转到 editProfileFragment、更改用户数据然后通过导航操作返回 profileFragment 时,我无法从 profileFragment 再次到达 editProfileFragment。我收到了这个错误:
Navigation action/destination xxx:id/action_profileFragment_to_editProfileFragment cannot be found from the current destination Destination(xxx:id/editProfileFragment)
我正在尝试使用 MVVM 架构,所以我的导航是这样的——在片段中我观察到 LiveData:
navController = Navigation.findNavController(view)
viewModel.navigateTo.observe(viewLifecycleOwner, EventObserver {
navController.navigate(it)
})
视图模型“导航到”:
private val _navigateTo = MutableLiveData<Event<Int>>()
val navigateTo: LiveData<Event<Int>> = _navigateTo
和导航方法:
fun goBackToProfile(){
_navigateTo.value = Event(R.id.action_editProfileFragment_to_profileFragment)
}
fun editProfileButtonClick() {
_navigateTo.value = Event(R.id.action_profileFragment_to_editProfileFragment)
}
我还使用 Jose Alcerreca 的 Event wrapper 类:
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let {
onEventUnhandledContent(it)
}
}
}
我不知道是否由于事件包装器而发生错误,或者我在导航方面做错了什么。我将不胜感激任何建议。
编辑:导航.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/main_nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="xxx.mainPackage.views.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/profileFragment"
android:name="xxx.mainPackage.views.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
<action
android:id="@+id/action_profileFragment_to_editProfileFragment"
app:destination="@id/editProfileFragment" />
</fragment>
<fragment
android:id="@+id/editProfileFragment"
android:name="xxx.mainPackage.views.EditProfileFragment"
android:label="fragment_edit_profile"
tools:layout="@layout/fragment_edit_profile" >
<action
android:id="@+id/action_editProfileFragment_to_chooseImageFragment"
app:destination="@id/chooseImageFragment" />
<action
android:id="@+id/action_editProfileFragment_to_profileFragment"
app:destination="@id/profileFragment" />
</fragment>
<fragment
android:id="@+id/chooseImageFragment"
android:name="xxx.mainPackage.views.ChooseImageFragment"
android:label="ChooseImageFragment" >
<action
android:id="@+id/action_chooseImageFragment_to_editProfileFragment"
app:destination="@id/editProfileFragment" />
</fragment>
</navigation>
解决方案
在我看来,您的导航方式错误。
首先
你正在从ProfileFragment
到EditProfileFragment
。然后,您从id 为 的操作EditProfileFragment
移至。ProfileFragment
R.id.action_profileFragment_to_editProfileFragment
我看到你从 Fragment 定义的那个动作ProfileFragment
,不是EditFragment
。
确保触发器 id 中的 LiveDatanavigateTo
是您想要从.EditProfileFragment
R.id.action_editProfileFragment_to_profileFragment
ProfileFragment
EditProfileFragment
第二
当您从 返回时EditProfileFragment
,不要调用navController.navigate()
,因为它会将您当前的片段保留在后台堆栈中并将您的目标片段推送到后台堆栈。如果您想返回而不将当前片段保留在后台堆栈中,请调用navController.popBackStack()
.
推荐阅读
- cassandra - 慢超时 500 毫秒/跨节点警告
- java - 配对游戏 - 卡片不要翻转
- maven - 如何在 mvn 依赖树命令中仅列出直接依赖项?
- rust - 您如何处理“无法将代码块解析为 Rust 代码”的 rustdoc 警告?
- c++ - 重载 >> 运算符未在输入上设置属性
- arrays - 比较数组的第一个元素
- php - 在 laravel 中使用 Faker 时,如何测试 API 调用的结果?
- scala - 为什么此代码会生成 scala 匹配异常
- docker - 如何使用 Traefik 将主机名(子域)映射到 Docker 容器?
- vb.net - 我可以重复使用表单来声明多个变量吗?