使用Android导航架构组件重新创建堆栈 [英] Recreating backstack with Android Navigation Architecture Component

本文介绍了使用Android导航架构组件重新创建堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用通知中的PendingIntent来实现到应用程序的特定详细信息"页面的导航,但是从详细信息"页面一直到起始目标重新创建后退堆栈时,我遇到了问题.

I am trying to implement navigation to specific Detail pages of my app using PendingIntent from a notification, however I am having problems recreating the backstack from the Detail page all the way back to the start destination.

我在此处制作了一个示例应用,其中有一个活动和三个片段来演示:

I made a sample app here with a single activity and three fragments to demo this:

Fragment 1   ->   Fragment 2  ->  Fragment 3
(start dest) <-               <-

从片段1(起始目标)开始,我使用

From Fragment 1 (the start destination), I navigate directly to Fragment 3 using

    Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.fragment2,
            null,
            NavOptions.Builder()
                    .build())

在片段3中,当我呼叫Navigation.findNavController(this,R.id.nav_host_fragment).navigateUp()时,我导航回到片段1.是否有办法让它导航到新创建的片段2?

From Fragment 3, when I call Navigation.findNavController(this,R.id.nav_host_fragment).navigateUp() I am navigated back to Fragment 1. Is there a way to get this to navigate to a newly created Fragment 2 instead?

推荐答案

感谢M.G指向正确的方向,我已经设法通过使用导航库手动创建后退堆栈来解决此问题.这意味着需要多次依次调用findNavController(...).navigate(...)来创建后退堆栈.

Thanks to M.G for pointing to the right direction, I have managed to solve this by manually creating the backstack using the navigation library. This means sequentially calling findNavController(...).navigate(...) multiple times to create a backstack.

例如,当我深入链接到片段3,但想要向上导航回到片段1和2时,我会调用:

For example when I deep link to fragment 3 but want an up navigation back to fragments 1 and 2, I call:

findNavController.navigate(R.id.fragment1, ...)
findNavController.navigate(R.id.fragment2, ...)
findNavController.navigate(R.id.fragment3, ...)

这篇关于使用Android导航架构组件重新创建堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆