什么是'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是对Bundle对象的引用,该对象被传递到每个Android Activity的onCreate方法中。
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将为null。
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.
例如,savedInstanceState在第一次启动Activity时始终为null,但如果在循环期间销毁Activity,则可能为非null,因为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屋!