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

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

问题描述

我试图保存和恢复一个活动的状态使用方法的onSaveInstanceState() onRestoreInstanceState()

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

现在的问题是,它不会进入 onRestoreInstanceState()方法。谁能给我解释一下这是为什么?

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

推荐答案

通常你恢复的onCreate您的状态()。这是可能给它的 onRestoreInstanceState恢复()为好,但不是很普遍。 ( onRestoreInstanceState() ONSTART被称为(),而的onCreate() ONSTART被称为()

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().

使用看跌期权的方法来存储值的onSaveInstanceState()

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

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

而在的onCreate还原值()

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

您不必存储视图状态,因为它们会自动存储。

You do not have to store view states, as they are stored automatically.

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

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