如何从Android中的onSaveInstanceState包中获取数据

时间:2014-06-06 05:58:13

标签: android

您好我正在尝试从Android中的onSaveInstanceState Activity捆绑中获取数据。

我希望获得捆绑数据中的此属性curTab的值。

捆绑数据在这里:

Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@4178b1e8, 16908294=android.view.AbsSavedState$1@4178b1e8, 16908305=android.view.AbsSavedState$1@4178b1e8, 16908306=FragmentTabHost.SavedState{426e3158 curTab=parent2}, 16908307=android.view.AbsSavedState$1@4178b1e8, 16908310=android.view.AbsSavedState$1@4178b1e8, 2131492923=android.view.AbsSavedState$1@4178b1e8, 2131492993=android.view.AbsSavedState$1@4178b1e8, 2131493010=android.view.AbsSavedState$1@4178b1e8, 2131493204=android.view.AbsSavedState$1@4178b1e8, 2131493205=android.view.AbsSavedState$1@4178b1e8, 2131493206=android.view.AbsSavedState$1@4178b1e8, 2131493207=android.view.AbsSavedState$1@4178b1e8, 2131493208=android.view.AbsSavedState$1@4178b1e8, 2131493209=android.view.AbsSavedState$1@4178b1e8, 2131493210=android.widget.ProgressBar$SavedState@426e3140, 2131493211=android.view.AbsSavedState$1@4178b1e8}, android:focusedViewId=2131493221}], android:support:fragments=android.support.v4.app.FragmentManagerState@426e4770}]

我试图像这样curTab,但我得到了空。

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.d("Demo", outState.getString("curTab"));
}

修改

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("Demo", "onDestroy");
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("current_tab_selected", mTabHost.getCurrentTab());
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  int selectedTab = savedInstanceState.getInt("current_tab_selected");
  Log.d("Demo", selectedTab + "");
}

@Override
public void onCreate(Bundle savedInstanceState) {

    ...

    if(savedInstanceState != null) {
        int selectedTabIndex = savedInstanceState.getInt("current_tab_selected");
        mTabHost.setCurrentTab(selectedTabIndex);
    }
}

请提前帮助和谢谢。

3 个答案:

答案 0 :(得分:4)

您无法在onSaveInstanceState();

中获取数据

您需要使用onRestoreInstanceState()代替onSaveInstanceState();

像这样:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {

  super.onRestoreInstanceState(savedInstanceState);
  String str = savedInstanceState.getString("curTab");

}

如果onCreate()对象包含Bundle,您也可以在活动curTab中访问此内容。

 @Override
    public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

       if(savedInstanceState != null)
          String str = savedInstanceState.getString("curTab");

    }

答案 1 :(得分:2)

您将数据保存在onSaveInstanceState()并检索onRestoreInstanceState()中的数据这是一个示例

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putString("MyString", "Welcome back to Android");
  // etc.
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  String myString = savedInstanceState.getString("MyString");
}

答案 2 :(得分:0)

public String strfoo =“”;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main2);

        //comment this line you only fatch values from savedInstanceState


        String myString = savedInstanceState.getString("foo");

        Log.i("debug", "saved data: " + myString);

        // savedInstanceState.putString("foo", "bar");
        strfoo="bar"; set value here
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      String myString = savedInstanceState.getString("foo");
      Log.i("debug", "saved data: " + myString);
    }

  @Override

  public void onSaveInstanceState(Bundle savedInstanceState) {
     savedInstanceState.putString("foo", strfoo);
     super.onSaveInstanceState(savedInstanceState);

   }