android - 导航组件有条件的popUpTo?
问题描述
导航组件的新手。我正在努力实现这一目标,但我不知道如何:
我有3个片段:
FragmentA
→ FragmentList
→FragmentAdd
如果我尝试从FragmentMain
toFragmentList
但列表为空,它应该直接转到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 导航组件的条件后退导航的不同之处在于我可以将内容添加到FragmentList
fromFragmentAdd
请问有什么建议吗?
解决方案
app:popUpTo="@id/FragmentList" and app:popUpToInclusive="true"
在 action_fragmentList_to_fragmentAdd 中使用
<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>
推荐阅读
- python - 在 Coles.com.au 上抓取产品链接 429 错误,有 1 个请求
- firebase - 如果 firebase 项目中有多个应用程序,firebase 函数如何识别部署它们的应用程序?
- jmeter - 如何减少 JMeter 中的计数器
- amazon-web-services - 如何在 aws codebuild 中基于 $CODEBUILD_BUILD_SUCCEEDING 环境变量使构建失败?
- python - Word Counter 循环在 Python 中不断加载
- puppeteer - 如何制作监视功能以等待 puppeteer 中的 html 元素
- javascript - 多个变量与一个对象之间的差异
- database - 在单个文件中实现数据库
- swift - curl 到 URLRequest - 一个工作,一个不工作
- angular - Owl Carousel Slider 在 Angular 组件中不工作,但在 Index.html 中工作