本文介绍了为什么我的片段的onSaveInstanceState()被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,它有自己的状态(选择按钮等)。该国是失去了一个屏幕旋转。

I have a fragment which has its own state (selected buttons, etc). That state is lost on a screen rotation.

这包含片段的活性在/ RES /布局/纵向版式,和/ RES /布局陆/几乎相同的景观布置。这两种布局包括片段,像这样:

The activity that contains the fragment has a portrait layout in /res/layout/, and an almost identical landscape layout in /res/layout-land/. Both layouts include the fragment like so:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <fragment
        android:id="@+id/testFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="au.com.x.y.TestFragment" />
...</LinearLayout>

该片段类我一直在测试用的是:

The fragment class I've been testing with is:

public class TestFragment extends android.support.v4.app.Fragment {
    private static final String TAG = "TestFragment";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate(): " +
                (savedInstanceState != null ? "NOT NULL" : "NULL"));
    }
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated(): " +
                (savedInstanceState != null ? "NOT NULL" : "NULL"));
    }
    public void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState(state);
        Log.i(TAG, "onSaveInstanceState()");
        state.putString("saved_thing", "some_value");
    }
    public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle b) { ... }
}

请注意,我使用了pre-3.0的支持包碎片,通过ActionBarSherlock进来。

Note that I'm using the pre-3.0 support package for fragments, brought in through ActionBarSherlock.

LogCat中给我:

LogCat gives me:

BEFORE SCREEN ROTATION (PORTRAIT):
02-23 11:45:58.015: I/TestFragment(22629): onCreate(): NULL
02-23 11:45:58.015: I/TestFragment(22629): onCreateView()
02-23 11:45:58.035: I/TestFragment(22629): onActivityCreated(): NULL
AFTER SCREEN ROTATION (LANDSCAPE):
02-23 11:46:00.615: I/TestFragment(22629): onCreate(): NULL
02-23 11:46:00.615: I/TestFragment(22629): onCreateView()
02-23 11:46:00.635: I/TestFragment(22629): onActivityCreated(): NULL

正如你所看到的,的onSaveInstanceState()不会被调用,而该片段总是得到一个空 savedInstanceState ,在这两个的onCreate() onActivityCreated()

As you can see, onSaveInstanceState() is never called, and the fragment always gets a null savedInstanceState, in both onCreate() and onActivityCreated().

我已经试过 myFragment.setRetainInstance(真)在我的活动的onCreate(),但这不是招 T改为什么。

I've tried myFragment.setRetainInstance(true) in my activities onCreate(), but that hasn't changed anything.

我的的onSaveInstanceState()有一个@Override,所以我知道这是不是傻得像一个错字。

My onSaveInstanceState() has an @Override, so I know it's not something stupid like a typo.

我看的ActionBarSherlock例子(com.actionbarsherlock.sample.shakespeare)和碎片一个人也没有被其正常调用它们的的onSaveInstanceState()的方法。据我所知,这个例子是写完全和我code是&mdash;片段包括通过双方的布局和布局土地XML。我已经使用ActionBarSherlock完全相同的版本作为我的主要项目使用,并保存实例状态能正常工作的例子甚至建立了样品。

I've looked at one of the ActionBarSherlock examples (com.actionbarsherlock.sample.shakespeare) and the fragments there are having their onSaveInstanceState() methods called properly. As far as I can tell, that example is written exactly how my code is — fragments included through both a layout and a layout-land XML. I've even built that sample using exactly the same version of ActionBarSherlock as my main project is using, and saving the instance state works fine for that example.

我如何保留我的碎片状态,整个屏幕旋转?为什么不是的onSaveInstanceState()被称为?

How do I retain my fragments state across screen rotations? Why isn't onSaveInstanceState() being called?

推荐答案

您可以启用 myFragment.setRetainInstance(真)来保持状态,但它确实该自动',也就是说,它保留了分配给你的类成员的值,并且不使用的onSaveInstanceState(因此savedInstanceState始终为空)。

You can enable myFragment.setRetainInstance(true) to retain the state, but it does this 'automatically', i.e. it retains the values assigned to your class members and does NOT use the onSaveInstanceState (and hence savedInstanceState is always null).

请确保该 FragmentActivity 承载此片段不覆盖的onSaveInstanceState 或当它应该叫 super.onSaveInstanceState(捆绑)

Make sure the FragmentActivity that hosts this fragment does not override onSaveInstanceState or when it does it should call super.onSaveInstanceState(Bundle).

这篇关于为什么我的片段的onSaveInstanceState()被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:30