【问题标题】:How to get data from bundle of onSaveInstanceState in Android如何从 Android 中的 onSaveInstanceState 包中获取数据
【发布时间】:2014-06-06 14:40:17
【问题描述】:

您好,我正在尝试从 Android 中的 onSaveInstanceStateActivity 的捆绑包中获取数据。

我希望获取捆绑数据中此属性 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);
    }
}

请提前帮助和感谢。

【问题讨论】:

  • 尝试在super.onSaveInstanceState之前调用它
  • 使用putString("curTab","xyz");设置此curTab数据的位置
  • @TqT 不,它没有用
  • @LavekushAgrawal 我没有在任何地方设置它,但捆绑包含我想要检索的 curTab

标签: android


【解决方案1】:

onSaveInstanceState();无法获取数据

您需要使用onRestoreInstanceState() 而不是onSaveInstanceState();

像这样:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {

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

}

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

 @Override
    public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

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

    }

【讨论】:

  • wut 是否可以在 onCreate 中调用super.onRestoreInstanceState(savedInstanceState);
  • 在我的情况下 onRestoreInstanceState 没有调用。为什么会这样?
  • 当我尝试进入 onCreate 时,我得到了 savedInstanceState 为空
  • 你能告诉我你调试的是哪个包对象吗?我已经编辑了我的答案。
  • 请参阅我更新了问题,在我的情况下,它已经调用 onSaveInstanceState 然后 onDestroy 然后 onCreate 然后 onCreate 它正在将 savedBundeInstanceState 设置为 null
【解决方案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");
}

【讨论】:

  • 就我而言,onRestoreInstanceState 没有打电话。为什么会这样?
  • 当我试图进入 onCreate 时,我得到了 savedInstanceState 为空
  • 因为您的活动需要在被调用之前被销毁。只需更改手机的方向,这两种方法都会被调用。
  • 但它已经在调用onSaveInstanceState 然后onDestroy 然后onCreate
  • @Williams 然后在create() 之后,它会自动调用onRestoreInstanceState()。看看这个链接:developer.android.com/training/basics/activity-lifecycle/…
【解决方案3】:

公共字符串 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);

   } 

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多