问题描述
我编写了一个在两个片段之间切换的虚拟活动.当您从 FragmentA 转到 FragmentB 时,FragmentA 被添加到后堆栈中.但是,当我返回到 FragmentA(通过按回)时,会创建一个全新的 FragmentA 并且它所处的状态丢失了.我觉得我和这个问题一样,但我已经包含了一个完整的代码示例来帮助解决这个问题:
I've written up a dummy activity that switches between two fragments. When you go from FragmentA to FragmentB, FragmentA gets added to the back stack. However, when I return to FragmentA (by pressing back), a totally new FragmentA is created and the state it was in is lost. I get the feeling I'm after the same thing as this question, but I've included a complete code sample to help root out the issue:
public class FooActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, new FragmentA());
transaction.commit();
}
public void nextFragment() {
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, new FragmentB());
transaction.addToBackStack(null);
transaction.commit();
}
public static class FragmentA extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View main = inflater.inflate(R.layout.main, container, false);
main.findViewById(R.id.next_fragment_button).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((FooActivity) getActivity()).nextFragment();
}
});
return main;
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save some state!
}
}
public static class FragmentB extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.b, container, false);
}
}
}
添加了一些日志消息:
07-05 14:28:59.722 D/OMG ( 1260): FooActivity.onCreate
07-05 14:28:59.742 D/OMG ( 1260): FragmentA.onCreateView
07-05 14:28:59.742 D/OMG ( 1260): FooActivity.onResume
<Tap Button on FragmentA>
07-05 14:29:12.842 D/OMG ( 1260): FooActivity.nextFragment
07-05 14:29:12.852 D/OMG ( 1260): FragmentB.onCreateView
<Tap 'Back'>
07-05 14:29:16.792 D/OMG ( 1260): FragmentA.onCreateView
它从不调用 FragmentA.onSaveInstanceState 并且在您回击时创建一个新的 FragmentA.但是,如果我在 FragmentA 上并锁定屏幕,FragmentA.onSaveInstanceState 确实会被调用.太奇怪了......我期望添加到后台堆栈中的片段不需要重新创建是错误的吗?以下是 docs 的说明:
It's never calling FragmentA.onSaveInstanceState and it creates a new FragmentA when you hit back. However, if I'm on FragmentA and I lock the screen, FragmentA.onSaveInstanceState does get called. So weird...am I wrong in expecting a fragment added to the back stack to not need re-creation? Here's what the docs say:
然而,如果您在删除片段时确实调用了 addToBackStack(),然后片段停止并在用户导航时恢复回来了.
推荐答案
如果你从后栈返回一个片段,它不会重新创建片段而是重新使用相同的实例并以 onCreateView()
在fragment生命周期中,参见Fragment生命周期.
If you return to a fragment from the back stack it does not re-create the fragment but re-uses the same instance and starts with onCreateView()
in the fragment lifecycle, see Fragment lifecycle.
所以如果你想存储状态,你应该使用实例变量,不要依赖onSaveInstanceState()
.
So if you want to store state you should use instance variables and not rely on onSaveInstanceState()
.
这篇关于添加到返回堆栈时如何保持片段状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!