简体   繁体   English

什么是“savedInstanceState”?

[英]What is 'savedInstanceState'?

I am new to Android Development but I have some knowledge of OOP concepts.我是 Android 开发的新手,但我对 OOP 概念有一些了解。 I am trying to learn Android from Java perspective.我正在尝试从 Java 的角度学习 Android。

我的代码

I understand that savedInstanceState inside onCreate() on line 9 is the declaration of Bundle class.我知道第 9 行onCreate()中的savedInstanceState是 Bundle 类的声明。 On line 10 we are calling the onCreate() method from the super class.在第 10 行,我们从超类调用onCreate()方法。

Here is what I don't unterstand: On line 10 we pass savedInstanceState itself as a parameter to the onCreate() method.这是我不明白的:在第 10 行,我们将savedInstanceState作为参数传递给onCreate()方法。 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.这对我来说没有意义,因为我希望传递一个Bundle类型的对象,但我们将引用而不是Bundle类型的对象传递给方法。

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. savedInstanceState 是对传递到每个 Android Activity 的 onCreate 方法的 Bundle 对象的引用。

The onCreate() expects to be called with a Bundle as parameter so we pass savedInstanceState. onCreate() 期望以 Bundle 作为参数被调用,因此我们传递了 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.如果没有可用的实例数据,则savedInstanceState 将为空。

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.例如,第一次启动 Activity 时,savedInstanceState 将始终为 null,但如果 Activity 在轮换期间被销毁,则可能为非 null,因为每次 Activity 启动或重新启动时都会调用 onCreate。

Hope it helps希望它有帮助

When an activity is re-initialized ,then the savedInstance contains the most recent data , specially contains data of the activity's previous initialization part.当一个活动被重新初始化时,那么savedInstance 包含最近的数据,特别是包含活动之前初始化部分的数据。

Actually, when an activity is killed it saved its states so that when user back to this activity , it can restore its states实际上,当一个活动被杀死时,它会保存它的状态,这样当用户回到这个活动时,它可以恢复它的状态

Basically bundle is a type of data structure which stores the data which was used previously in the activity..When there is some disturbance by the system or suppose some called u and u picked up phone then the data u had in ur app kept stored in bundle and you can access the same data..基本上 bundle 是一种数据结构,它存储先前在活动中使用的数据。捆绑,您可以访问相同的数据..

And if we talk about savedinstancestate then it is very easy to know that basically savedinstancestate is an object/reference what ever doesn't matter but it is the way by which data will be kept stored...As u must know that class is just a prototype or blueprint (you may say) savedinstancestate is an object or reference.. Bundle is a class I think u know that..如果我们谈论savedinstancestate 那么很容易知道基本上savedinstancestate 是一个对象/引用,这无关紧要,但它是保存数据的方式……因为您必须知道该类只是一个原型或蓝图(你可能会说)savedinstancestate 是一个对象或引用.. Bundle 是一个类,我想你知道..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM