如何保持片段视图状态? [英] How to keep fragment view state?
问题描述
我对片段系统有疑问.
我有两个片段,例如 A和B
如果我将 A 移至 B ,
Navigation.findNavController(v).navigate(R.id.B)
现在 A 片段onDestroyView被称为我知道这是正常的.在 B 片段中,我调用了PopBackStack
Now A fragment onDestroyView is called i know it's normal. After in B Fragment i called PopBackStack
Navigation.findNavController(v).popBackStack()
现在一个片段onViewCreated被称为我也知道它是正常的.现在 A 片段将所有ui设为初始状态.
now A fragment onViewCreated is called i also know it's normal. Now A fragment all ui is initial state.
我的问题是如何保持片段UI状态,例如recyclerview滚动位置,FAB按钮可见性等
推荐答案
您必须将recyclerview滚动位置,FAB按钮可见性存储在变量中,并在调用onViewCreated之后设置这些值.
You have to store recyclerview scroll position, FAB button visibility in a variable and set those values after onViewCreated is called.
OR
您可以将其存储在onSaveInstanceState的Bundle中并保留其状态
You can store it in Bundle of onSaveInstanceState and retain its state
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
这篇关于如何保持片段视图状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!