使用 Navigation Architecture 组件导航到另一个片段时保持片段状态
Maintaining the state of fragment when navigating to other fragment using Navgation Architechture component
在我的项目中,我使用导航组件在片段之间导航。但是当我从一个片段导航到另一个片段时,前一个片段在返回时失去了它的状态。
例如。我有片段 A 并在它的 editText 中写了一些东西,然后我导航到另一个片段 B。但是当我使用 backpress 回到我的片段 A 时,片段 A 中的 editText 是空的。
是否有解决此问题的方法或解决方案?
根据 open issue,Navigation 不直接支持多个返回堆栈 - 即,当您从 A 或 C 返回 B 时保存堆栈 B 的状态,因为 Fragments 不支持多个返回堆栈。
根据this comment:
The NavigationAdvancedSample is now available at https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample
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.
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 添加直接支持时获得更新。
在我的项目中,我使用导航组件在片段之间导航。但是当我从一个片段导航到另一个片段时,前一个片段在返回时失去了它的状态。
例如。我有片段 A 并在它的 editText 中写了一些东西,然后我导航到另一个片段 B。但是当我使用 backpress 回到我的片段 A 时,片段 A 中的 editText 是空的。
是否有解决此问题的方法或解决方案?
根据 open issue,Navigation 不直接支持多个返回堆栈 - 即,当您从 A 或 C 返回 B 时保存堆栈 B 的状态,因为 Fragments 不支持多个返回堆栈。
根据this comment:
The NavigationAdvancedSample is now available at https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdvancedSample
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.
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 添加直接支持时获得更新。