本文介绍了Android 导航组件:如何保存片段状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 bottomNavigationView 和导航组件.请告诉我如何在切换到另一个选项卡并返回旧选项卡后不破坏片段?例如,我有三个选项卡 - A、B、C.我的开始选项卡是 A.导航到 B 后,然后返回 A.当我返回选项卡 A 时,我不希望它被重新创建.怎么办?谢谢

I use bottomNavigationView and navigation component. Please tell me how I don't destroy the fragment after switching to another tab and return to the old one? For example I have three tabs - A, B, C. My start tab is A. After I navigate to B, then return A. When I return to tab A, I do not want it to be re-created. How do it? Thanks

推荐答案

根据 未决问题, Navigation 不直接支持多回退栈——即从 A 或 C 回退到 B 时保存栈 B 的状态,因为 Fragment 不支持多回退栈.

As per the open issue, Navigation does not directly support multiple back stacks - i.e., saving the state of stack B when you go back to B from A or C since Fragments do not support multiple back stacks.

根据此评论:

NavigationAdvancedSample 现在可在 https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample

此示例使用多个 NavHostFragment,每个底部导航选项卡一个,以解决 Fragment API 在支持多个返回堆栈方面的当前限制.

This sample uses multiple NavHostFragments, one for each bottom navigation tab, to work around the current limitations of the Fragment API in supporting multiple back stacks.

我们将继续使用 Fragment API 以支持多个返回堆栈,并在创建后将导航 API 插入其中,这将消除对 NavigationExtensions.kt 文件之类的任何需要.我们将继续使用此问题来跟踪该工作.

We'll be proceeding with the Fragment API to support multiple back stacks and the Navigation API to plug into it once created, which will remove the need for anything like the NavigationExtensions.kt file. We'll continue to use this issue to track that work.

因此,您现在可以在您的应用中使用 NavigationAdvancedSample 方法并为问题加注星标,以便您在解决基本问题并向导航添加直接支持时获得更新.

Therefore you can use the NavigationAdvancedSample approach in your app right now and star the issue so that you get updates for when the underlying issue is resolved and direct support is added to Navigation.

这篇关于Android 导航组件:如何保存片段状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 02:16