来自 savedInstanceState 的数据在 Android 中始终为空
Data from savedInstanceState is always empty in Android
我正在开发 Android 应用程序。我正在 activity 中使用 onSaveInstanceState 事件进行测试。但它没有像我预期的那样工作,因为当我在 onCreate 事件中检索它时,值字符串总是空的。
这是我的activity
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("message","Hello world!");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null)
{
Toast.makeText(getBaseContext(),savedInstanceState.getString("message"),Toast.LENGTH_SHORT).show();
//Log.i("DATA_FROM_STATE",savedInstanceState.getString("message"));
}
}
当我运行 activity 和改变方向时,它总是敬酒空信息。当我测试取消注释日志行时,它给我错误,因为数据字符串为空或 null。为什么它不起作用?如何正确保存和检索?
方法覆盖错误。应该使用 onSaveInstanceState(Bundle outState) 并且它会起作用。您可以复制以下代码段并进行测试。
public class MainActivity extends AppCompatActivity {
private final String SAVED_MESSAGE_KEY = "SAVED_MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null) {
if(savedInstanceState.containsKey(SAVED_MESSAGE_KEY)) {
Log.i("Saved message is:", savedInstanceState.getString(SAVED_MESSAGE_KEY));
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SAVED_MESSAGE_KEY, "Hello world!");
}
}
第一眼看到你在保存数据之前调用了super
我猜你的应用程序是 运行 API21+ 版本 Android,因为它需要调用你的方法
public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);
@Override
public void onSaveInstanceState(Bundle outState,PersistableBundle outPersistentState) {
outState.putString("message","Hello world!");
super.onSaveInstanceState(outState, outPersistentState);
}
我正在开发 Android 应用程序。我正在 activity 中使用 onSaveInstanceState 事件进行测试。但它没有像我预期的那样工作,因为当我在 onCreate 事件中检索它时,值字符串总是空的。
这是我的activity
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("message","Hello world!");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null)
{
Toast.makeText(getBaseContext(),savedInstanceState.getString("message"),Toast.LENGTH_SHORT).show();
//Log.i("DATA_FROM_STATE",savedInstanceState.getString("message"));
}
}
当我运行 activity 和改变方向时,它总是敬酒空信息。当我测试取消注释日志行时,它给我错误,因为数据字符串为空或 null。为什么它不起作用?如何正确保存和检索?
方法覆盖错误。应该使用 onSaveInstanceState(Bundle outState) 并且它会起作用。您可以复制以下代码段并进行测试。
public class MainActivity extends AppCompatActivity {
private final String SAVED_MESSAGE_KEY = "SAVED_MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null) {
if(savedInstanceState.containsKey(SAVED_MESSAGE_KEY)) {
Log.i("Saved message is:", savedInstanceState.getString(SAVED_MESSAGE_KEY));
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SAVED_MESSAGE_KEY, "Hello world!");
}
}
第一眼看到你在保存数据之前调用了super 我猜你的应用程序是 运行 API21+ 版本 Android,因为它需要调用你的方法
public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);
@Override
public void onSaveInstanceState(Bundle outState,PersistableBundle outPersistentState) {
outState.putString("message","Hello world!");
super.onSaveInstanceState(outState, outPersistentState);
}