我正在构建一个notes应用程序。我想在用户从createsNotesFragment按下后退按钮时保存笔记,并完成createsNotesFragment,以便我可以移动到homeFragment。我用来完成此操作的代码如下:
activity?.onBackPressedDispatcher?.addCallback(this){
saveNote()
activity?.supportFragmentManager?.popBackStack()
}
这是工作正常。但当我试图再次打开createNotesFragment添加另一个笔记时,它向我显示了这个错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lavanianotesapp, PID: 4952
java.lang.IllegalArgumentException: Navigation action/destination com.example.lavanianotesapp:id/action_homeFragment_to_createNotesFragment cannot be found from the current destination Destination(com.example.lavanianotesapp:id/createNotesFragment) label=fragment_create_notes class=com.example.lavanianotesapp.UI.Fragments.CreateNotesFragment
at androidx.navigation.NavController.navigate(NavController.kt:1540)
at androidx.navigation.NavController.navigate(NavController.kt:1472)
at androidx.navigation.NavController.navigate(NavController.kt:1454)
at androidx.navigation.NavController.navigate(NavController.kt:1437)
这是我的导航图
<?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/nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.lavanianotesapp.UI.Fragments.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_createNotesFragment"
app:destination="@id/createNotesFragment" />
<action
android:id="@+id/action_homeFragment_to_editNotesFragment"
app:destination="@id/editNotesFragment" />
</fragment>
<fragment
android:id="@+id/createNotesFragment"
android:name="com.example.lavanianotesapp.UI.Fragments.CreateNotesFragment"
android:label="fragment_create_notes"
tools:layout="@layout/fragment_create_notes" >
<action
android:id="@+id/action_createNotesFragment_to_homeFragment"
app:destination="@id/homeFragment" />
<action
android:id="@+id/action_createNotesFragment_to_addLabelFragment"
app:destination="@id/addLabelFragment" />
</fragment>
<fragment
android:id="@+id/editNotesFragment"
android:name="com.example.lavanianotesapp.UI.Fragments.EditNotesFragment"
android:label="fragment_edit_notes"
tools:layout="@layout/fragment_edit_notes" >
<action
android:id="@+id/action_editNotesFragment_to_homeFragment"
app:destination="@id/homeFragment" />
<action
android:id="@+id/action_editNotesFragment_to_addLabelFragment"
app:destination="@id/addLabelFragment" />
</fragment>
<fragment
android:id="@+id/addLabelFragment"
android:name="com.example.lavanianotesapp.UI.Fragments.AddLabelFragment"
android:label="fragment_add_label"
tools:layout="@layout/fragment_add_label" >
<action
android:id="@+id/action_addLabelFragment_to_createNotesFragment"
app:destination="@id/createNotesFragment" />
<action
android:id="@+id/action_addLabelFragment_to_editNotesFragment"
app:destination="@id/editNotesFragment" />
</fragment>
</navigation>
我不明白为什么在popBackStack()之后我不能再次打开createNotesFragment。有没有popBackStack的替代方法或者我错过了什么?
2条答案
按热度按时间sg3maiej1#
你可以试着检查你当前屏幕的当前目的地,像这样:
pbpqsu0x2#
nav图形文件,
用户从createsNotesFragment文件按下返回按钮,