本文介绍了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

推荐答案

按照公开问题,导航不直接支持多个后向堆栈-即,由于片段不支持多个后向堆栈,因此当您从A或C返回到B时,保存堆栈B的状态.

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.

根据此评论:

此示例使用多个NavHostFragments(每个底部导航选项卡一个)来解决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以支持多个后向堆栈,并在创建后将Navigation 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方法并为问题加注星标,以便在基础问题已解决并将直接支持添加到Navigation时获得更新.

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:17