导航组件条件弹出式?



新手在导航组件。我正在努力实现这一点,但我不知道如何:

我有三个片段:

FragmentMainFragmentListFragmentAdd

如果我尝试从FragmentMainFragmentList,但列表是空的,它应该直接到FragmentAdd(它不是真的直接,因为我在FragmentList上检查列表,但对于用户来说,它应该看起来像它)。如果我在FragmentAdd上,我回到(通过按后退按钮或回到工具栏),它应该回到FragmentMain(因为FragmentList仍然是空的),但如果我添加一些东西,它应该回到FragmentList

我试过了

<fragment
android:id="@+id/fragmentMain" >
<action android:id="@+id/action_fragmentMain_to_fragmentList
app:destination="@id/fragmentList"
app:popUpTo="@id/fragmentMain"
app:popUpToInclusive="true"/>
</fragment>

事情是,如果我按回FragmentAdd,它会去FragmentList,检查列表仍然是空的,回到FragmentAdd。如果我打开FragmentList,然后添加一些东西,它会进入FragmentMain,而不是列表。

与Android导航组件的条件返回导航的不同之处在于,我可以从FragmentAdd添加内容到FragmentList

有什么建议吗?

在action_fragmentList_to_fragmentAdd中使用app:popUpTo="@id/FragmentList" and app:popUpToInclusive="true"

<fragment
android:id="@+id/fragmentMain" >
<action android:id="@+id/action_fragmentMain_to_fragmentList
app:destination="@id/fragmentList"/>
</fragment>
<fragment
android:id="@+id/fragmentList" >
<action android:id="@+id/action_fragmentList_to_fragmentAdd
app:destination="@id/fragmentAdd"
app:popUpTo="@id/fragmentList"
app:popUpToInclusive="true"/>
</fragment>
<fragment
android:id="@+id/fragmentAdd" >
</fragment>

最新更新