首页 > 解决方案 > 导航到片段时应用程序崩溃

问题描述

我正在为用户配置文件功能使用 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>

标签: androidkotlinnavigation

解决方案


在我看来,您的导航方式错误。

首先

你正在从ProfileFragmentEditProfileFragment。然后,您从id 为 的操作EditProfileFragment移至。ProfileFragmentR.id.action_profileFragment_to_editProfileFragment

我看到你从 Fragment 定义的那个动作ProfileFragment,不是EditFragment

确保触发器 id 中的 LiveDatanavigateTo是您想要从.EditProfileFragmentR.id.action_editProfileFragment_to_profileFragmentProfileFragmentEditProfileFragment

第二

当您从 返回时EditProfileFragment,不要调用navController.navigate(),因为它会将您当前的片段保留在后台堆栈中并将您的目标片段推送到后台堆栈。如果您想返回而不将当前片段保留在后台堆栈中,请调用navController.popBackStack().


推荐阅读