为什么onCreate Bundle和onSaveInstanceState Bundle在片段中有所不同?

时间:2015-08-30 11:19:46

标签: android fragment bundle restore oncreate

我环顾四周但找不到这个问题的答案。

在活动中这个简单的程序:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState!=null)Log.e("SaveInstanceState", "onCreate Bundle " + savedInstanceState.hashCode()+" " + this.getClass().getSimpleName());
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.e("SaveInstanceState", "onSaveInstanceState Bundle " + outState.hashCode()+" " + this.getClass().getSimpleName());

}

将导致以下输出(当我转动手机方向时):

E/SaveInstanceState﹕ onSaveInstanceState Bundle 1110617176 MainActivity
E/SaveInstanceState﹕ onCreate Bundle 1110617176 MainActivity

Same Bundle,因此我可以轻松传递和恢复数据。

但是在一个片段中,这段代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState!=null)Log.e("SaveInstanceState", "onCreate Bundle " + savedInstanceState.hashCode()+" " + this.getClass().getSimpleName());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater,container, savedInstanceState);
    if(savedInstanceState!=null)Log.e("SaveInstanceState", "onCreateView Bundle " + savedInstanceState.hashCode()+" " + this.getClass().getSimpleName());
    return inflater.inflate(R.layout.fragment_b, container, false);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.e("SaveInstanceState", "onSaveInstanceState Bundle " + outState.hashCode()+" " + this.getClass().getSimpleName());

}

此输出结果:

E/SaveInstanceState﹕ onSaveInstanceState Bundle 1110622064 FragmentB
E/SaveInstanceState﹕ onCreate Bundle 1110623448 FragmentB
E/SaveInstanceState﹕ onCreateView Bundle 1110623448 FragmentB

onSaveInstanceState Bundle和onCreate是不同的,因此我放松了存储在onSaveInstanceState Bundle中的数据。

你知道我做错了什么吗?

更新

好的,谢谢@Sascha Kolberg,Bundle Hashcode不同的事实并不意味着Bundle是空的。 然后问题变成:

为什么我无法从Fragment onCreate Bundle中检索我的数据?

这就是我的所作所为:

    public class FragmentB extends Fragment {

        public static final String USER_TEXT = "user_text";
        EditText userText;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            View layout = inflater.inflate(R.layout.fragment_b, container, false);
            userText = (EditText) layout.findViewById(R.id.userText);

            if (savedInstanceState != null) {
                Log.e("SaveInstanceState",
                      "onCreateView savedInstanceState " + savedInstanceState + " " + this.getClass().getSimpleName());
                Log.e("SaveInstanceState",
                      "onCreateView savedInstanceState.getString(USER_TEXT) " + savedInstanceState.getString(USER_TEXT) +
                      " " + this.getClass().getSimpleName());
                userText.setText(savedInstanceState.getString(USER_TEXT));
            }
            return layout;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState = new Bundle();
            outState.putString(USER_TEXT, String.valueOf(userText.getText()));
            Log.e("SaveInstanceState", "onSaveInstanceState outState " + outState + " " + this.getClass().getSimpleName());
            Log.e("SaveInstanceState",
                  "onSaveInstanceState outState.getString(USER_TEXT) " + outState.getString(USER_TEXT) + " " +
                  this.getClass().getSimpleName());
        }
    }

现在,我打开手机,写上“你好!”在EditText中,然后清除logcat,并将手机的方向从纵向更改为横向。

输出如下:

E/SaveInstanceState﹕ onSaveInstanceState outState Bundle[{user_text=Hello! }] FragmentB
E/SaveInstanceState﹕ onSaveInstanceState outState.getString(USER_TEXT) Hello!  FragmentB
E/SaveInstanceState﹕ onCreateView savedInstanceState Bundle[{android:view_state={2131492945=TextView.SavedState{423430e0 start=5 end=5 text=Hello!  locale=fr-FR}}}] FragmentB
E/SaveInstanceState﹕ onCreateView savedInstanceState.getString(USER_TEXT) null FragmentB

旋转手机后EditText为空。我猜我可以通过执行以下操作来检索数据:

SparseArray p=savedInstanceState.getSparseParcelableArray("android:view_state");

在onCreate方法中,但这看起来真的很复杂。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

活动 outState Bundle还包含其子片段的所有状态。

因此,当捆绑包传递onSaveInstanceState()中的某个片段时,它可能会在之后通过其兄弟姐妹并在' onCreate()'不仅仅包含自己的国家。