什么是“savedInstanceState"? [英] What is 'savedInstanceState'?
问题描述
我是 Android 开发的新手,但我对 OOP 概念有所了解.我正在尝试从 Java 的角度学习 Android.
I am new to Android Development but I have some knowledge of OOP concepts. I am trying to learn Android from Java perspective.
我知道第 9 行 onCreate()
中的 savedInstanceState
是 Bundle 类的声明.在第 10 行,我们从超类调用 onCreate()
方法.
I understand that savedInstanceState
inside onCreate()
on line 9 is the declaration of Bundle class. On line 10 we are calling the onCreate()
method from the super class.
这是我不明白的: 在第 10 行,我们将 savedInstanceState 本身作为参数传递给 onCreate()
方法.这对我来说没有意义,因为我希望传递一个 Bundle
类型的对象,但我们将引用而不是 Bundle 类型的对象传递给方法.>
Here is what I don't unterstand: On line 10 we pass savedInstanceState itself as a parameter to the onCreate()
method. This doesn't make sense to me as I would expect to pass an object which is of the Bundle
type, but instead of that we pass a reference not an object of type Bundle to the method.
推荐答案
savedInstanceState 是对传递到每个 Android Activity 的 onCreate 方法的 Bundle 对象的引用.
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity.
onCreate() 期望以 Bundle 作为参数被调用,因此我们传递了 savedInstanceState.
The onCreate() expects to be called with a Bundle as parameter so we pass savedInstanceState.
活动有能力在特殊情况下使用存储在此捆绑包中的数据将自身恢复到以前的状态.如果没有可用的实例数据,则savedInstanceState 将为空.
Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle. If there is no available instance data, the savedInstanceState will be null.
例如,在Activity第一次启动时,savedInstanceState将始终为null,但如果Activity在轮换期间被销毁,则可能为非null,因为每次Activity启动或重新启动时都会调用onCreate.
For example, the savedInstanceState will always be null the first time an Activity is started, but may be non-null if an Activity is destroyed during rotation, because onCreate is called each time activity starts or restarts.
希望能帮到你
这篇关于什么是“savedInstanceState"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!