- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Android 中,onCreate
方法将 savedInstanceState
作为 Bundle 对象的引用。我只想知道 Bundle
对象是在哪里以及如何创建的?
最佳答案
如果您将应用程序的状态保存在 bundle 中(通常是 onSaveInstanceState 中的非持久动态数据),则如果需要重新创建 Activity(例如,方向更改),则可以将其传回 onCreate,这样您就不需要不要丢失这些先前的信息。如果没有提供数据,savedInstanceState 为 null。
您需要重写 onSaveInstanceState(Bundle savingInstanceState) 并将要更改的应用程序状态值写入 Bundle 参数,如下所示:
@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.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
}
Bundle 本质上是一种存储 NVP(“名称-值对”)映射的方式,它将被传递到 onCreate() 和 onRestoreInstanceState(),您可以在其中提取值,如下所示:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
关于java - onCreate(Bundle savingInstanceState) 中创建的 Bundle 对象在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216219/
在 Android 中,onCreate 方法将 savedInstanceState 作为 Bundle 对象的引用。我只想知道 Bundle 对象是在哪里以及如何创建的? 最佳答案 如果您将应用程
我正在开发一个 Android 应用程序,并尝试在屏幕旋转时保存数据。我将此数据存储在名为 mCheatedResults 的数组中。这是一个将 boolean 数据和 int 配对的自定义对象数组。
我是一名优秀的程序员,十分优秀!