onSaveInstanceState() 和 onRestoreInstanceState() [英] onSaveInstanceState () and onRestoreInstanceState ()

查看:28
本文介绍了onSaveInstanceState() 和 onRestoreInstanceState()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 onSaveInstanceState()onRestoreInstanceState() 方法保存和恢复 Activity 的状态.>

问题在于它从不进入 onRestoreInstanceState() 方法.谁能向我解释这是为什么?

解决方案

通常你在 onCreate() 中恢复你的状态.也可以在 onRestoreInstanceState() 中恢复它,但不是很常见.(onRestoreInstanceState()onStart() 之后调用,而 onCreate()onStart() 之前调用.

使用 put 方法在 onSaveInstanceState() 中存储值:

protected void onSaveInstanceState(Bundle icicle) {super.onSaveInstanceState(冰柱);icicle.putLong("param", value);}

并恢复onCreate()中的值:

public void onCreate(Bundle icicle) {如果(冰柱!= null){value = icicle.getLong("param");}}

I'm trying to save and restore the state of an Activity using the methods onSaveInstanceState() and onRestoreInstanceState().

The problem is that it never enters the onRestoreInstanceState() method. Can anyone explain to me why this is?

解决方案

Usually you restore your state in onCreate(). It is possible to restore it in onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart().

Use the put methods to store values in onSaveInstanceState():

protected void onSaveInstanceState(Bundle icicle) {
  super.onSaveInstanceState(icicle);
  icicle.putLong("param", value);
}

And restore the values in onCreate():

public void onCreate(Bundle icicle) {
  if (icicle != null){
    value = icicle.getLong("param");
  }
}

这篇关于onSaveInstanceState() 和 onRestoreInstanceState()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆