(下面提供了 TLDR)
我有一个复杂的活动片段结构,之前是 3 个活动,现在转换为三个片段。因为它复杂、繁重并且需要交换大量数据,所以我将其更改为基于新的导航 UI 的结构,查看 Google 的向日葵,它现在更干净,工作速度更快更好。
My app's flow can easily be understood by the image provided below (Nav Graph provided below):
主要片段是Fragment1
其中包含ViewPager2
with FragmentStateAdapter
和里面的 5 个碎片。问题是Fragment1
当用户返回时完全重新加载Fragment2
.
两种可能的情况:
-
当用户打开时Fragment2
from Fragment1
然后回来,Fragment1
其中包含ViewPager
完全重新加载需要 2-3 秒,因为它包含 5Fragment
其中进一步包括Fragment
这就是我需要解决的问题。
-
正如我正在使用的ViewBinding
,如果我检查绑定是否已初始化(下面的代码)并传递初始化,则 Fragment1 不会重新加载but里面的碎片ViewPager
遇到任何 Fragment 中的任何按钮都会停止工作之类的问题,TabLayout
将停止工作,孩子ViewPager2
s in FragmentB
, FragmentD and FragmentE
将停止工作。
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
if (!this::binding.isInitialized) {
//initializing Binding here
}
return binding.root
}
-
Graph nav_main.xml
with pictorial representation:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_main"
app:startDestination="@id/fragment_1">
<fragment
android:id="@+id/fragment_1"
android:name="com.x.x.fragments.Fragment1"
tools:layout="@layout/fragment_1">
<action
android:id="@+id/action_fragment_1_to_fragment_2"
app:destination="@id/fragment_2"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
<action
android:id="@+id/action_fragment_1_to_fragment_3"
app:destination="@id/fragment_3"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
<fragment
android:id="@+id/fragment_2"
android:name="com.x.x.fragments.Fragment2"
tools:layout="@layout/fragment_2">
<action
android:id="@+id/action_fragment_2_to_fragment_3"
app:destination="@id/fragment_3"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
<argument
android:name="DataModel"
app:argType="com.x.x.models.DataModel" />
<argument
android:name="Tab"
app:argType="integer" />
</fragment>
<fragment
android:id="@+id/fragment_3"
android:name="com.x.x.fragments.Fragment3"
tools:layout="@layout/fragment_3">
<argument
android:name="DataModel"
app:argType="com.x.x.models.DataModel" />
</fragment>
</navigation>
-
活动_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_main" />
</layout>
(TL;DR)所以,我的问题是如何防止重新加载FragmentA
其中包含ViewPager2
并确保里面的Fragment不会停止工作?如果我只能阻止儿童重新加载,我没有问题FragmentC
,应用程序打开的默认值,其他应用程序可以重新加载。请注意,我已经阅读了几乎所有(有些)类似的问题和答案,但没有一个有帮助。
EDIT- 我尝试了两个答案这个问题 https://stackoverflow.com/q/47108494/8244632我想以此获得成功,但没有任何效果。
EDIT2- 发现一个类似的热门问题,没有任何可行的解决方案 -带有片段和 Jetpack 导航的 Viewpager2:恢复片段而不是重新创建它们 https://stackoverflow.com/q/57271553/8244632.
EDIT3- 我也尝试保存实例ViewPager
的适配器使用saveState()
in onStop()
and onSaveInstanceState()
并使用恢复它restoreState()
但随后,它只显示白屏。
EDIT4- 我也搜索了他们的 Github 问题,但没有找到解决方案。我唯一的解决方案是返回“活动”吗?难道和这些Fragment没有任何关系吗?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)