saveInstanceState始终为null

时间:2015-10-03 19:31:03

标签: android android-fragments android-activity

我目前有一个附加到活动的片段。当我旋转设备时,期望在再次创建视图时保存和恢复网格的细节和位置。 onSavedStateInstance()运行良好,我捆绑了所有内容,但是当调用片段的onCreateView()时,stateInstance为null。我知道之前已经提出并回答了类似的问题,但在我的案例中并没有真正帮助。以下是一些可能对调试有用的代码段。

MainActivityFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    if (savedInstanceState != null) {
        gridViewObjects = (List<GridViewObject>) savedInstanceState.get(movieKey);
        position = savedInstanceState.getInt("position");
    } else {
        gridViewObjects = new ArrayList<>();
    }
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList(movieKey, (ArrayList) gridViewObjects);
    outState.putInt("position", gridView.getFirstVisiblePosition());
    super.onSaveInstanceState(outState);
}

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.sunshine.android.example.com.popmovies.MainActivityFragment">

    <GridView
        android:id="@+id/fragment_main_gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" />

</FrameLayout>

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.indigo_500)));
    setContentView(R.layout.activity_main);
}

编辑:在调试时,我看到在设备方向更改时,将重新创建活动,并且OnCreate()方法中的savedInstanceState不为null。捆绑详细信息完好无损。只有在创建片段时,OnCreateView()中的savedInstanceState才为空。

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:0)

试试这个

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);    

         if (savedInstanceState != null) {
            gridViewObjects = (List<GridViewObject>) savedInstanceState.get(movieKey);
            position = savedInstanceState.getInt("position");
        } else {
            gridViewObjects = new ArrayList<>();
        }
    }

并且

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, null);
    return view;
}

答案 1 :(得分:0)

在片段中,通过覆盖onSaveInstanceState保存实例状态并在onActivityCreated上恢复:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's state here
    }
}
...
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

//Save the fragment's state here



}

重点是,在活动中,您必须在onSaveInstanceState上保存片段的实例并在onCreate上恢复。

public void onCreate(Bundle savedInstanceState) {
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's instance
        mContent = getSupportFragmentManager().getFragment(
                    savedInstanceState, "mContent");
        ...
    }
    ...
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

//Save the fragment's instance
getSupportFragmentManager().putFragment(outState, "mContent", mContent);



}

所以要正确保存Fragment的实例状态,您应该执行上面的代码