Android 什么是 onCreate(Bundle savedInstanceState)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10810418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's onCreate(Bundle savedInstanceState)
提问by user1077015
Can anyone help me to know about the Bundle savedInstanceState
in onCreate(Bundle savedInstanceState)
I am newbie in Android. I try to understand it from developer.android.com. But I am not able to understand. Can anyone simplify it?
谁能帮助我了解Bundle savedInstanceState
在onCreate(Bundle savedInstanceState)
我在Android中是新手。我试着从 developer.android.com 理解它。但我无法理解。任何人都可以简化它吗?
采纳答案by Dhruv Gairola
If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState
), it can be passed back to onCreate
if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState
is null.
如果您将应用程序的状态保存在一个包中(通常是 中的非持久性动态数据onSaveInstanceState
),onCreate
如果需要重新创建活动(例如,方向更改),则可以将其传递回,这样您就不会丢失此之前信息。如果没有提供数据,savedInstanceState
则为空。
... you should use the onPause() method to write any persistent data (such as user edits) to storage. In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.
...您应该使用 onPause() 方法将任何持久数据(例如用户编辑)写入存储。此外,在将活动置于这种后台状态之前调用方法 onSaveInstanceState(Bundle),允许您将活动中的任何动态实例状态保存到给定的 Bundle 中,以便稍后在 onCreate(Bundle) 中接收,如果活动需要重新创建。有关流程的生命周期如何与其托管的活动相关联的更多信息,请参阅流程生命周期部分。请注意,在 onPause() 而不是 onSaveInstanceState(Bundle) 中保存持久数据很重要,因为后者不是生命周期回调的一部分,因此不会在其文档中描述的所有情况下都被调用。
回答by Dheeresh Singh
onCreate(Bundle savedInstanceState)
you will get the Bundle
null when activity get starts first time and it will get in use when activity orientation get changed .......
onCreate(Bundle savedInstanceState)
Bundle
当活动第一次开始时你会得到空值,当活动方向改变时它会被使用.......
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
Android provides another elegant way of achieving this. To achieve this, we have to override a method called onSaveInstanceState()
. Android platform allows the users to save any instance state. Instance state can be saved in the Bundle. Bundle is passed as argument to the onSaveInstanceState method.
Android 提供了另一种优雅的方式来实现这一点。为了实现这一点,我们必须重写一个名为 的方法onSaveInstanceState()
。Android 平台允许用户保存任何实例状态。实例状态可以保存在 Bundle 中。Bundle 作为参数传递给 onSaveInstanceState 方法。
we can load the saved instance state from the Bundle passed as argument to the onCreate
method. We can also load the saved instance state in onRestoreInstanceState
method. But I will leave that for the readers to figure out.
我们可以从作为参数传递给onCreate
方法的 Bundle 加载保存的实例状态。我们还可以在onRestoreInstanceState
方法中加载保存的实例状态。但我会把它留给读者去弄清楚。
回答by Asad
As Dhruv Gairola answered, you can save the state of the application by using Bundle savedInstanceState. I am trying to give a very simple example that new learners like me can understand easily.
正如 Dhruv Gairola 回答的那样,您可以使用 Bundle savedInstanceState 来保存应用程序的状态。我试图举一个非常简单的例子,像我这样的新学习者可以很容易地理解。
Suppose, you have a simple fragment with a TextView and a Button. Each time you clicked the button the text changes. Now, change the orientation of you device/emulator and notice that you lost the data (means the changed data after clicking you got) and fragment starts as the first time again. By using Bundle savedInstanceState we can get rid of this. If you take a look into the life cyle of the fragment.Fragment Lifecylceyou will get that a method "onSaveInstanceState" is called when the fragment is about to destroyed.
假设您有一个带有 TextView 和 Button 的简单片段。每次单击按钮时,文本都会更改。现在,改变你的设备/模拟器的方向,并注意到你丢失了数据(意味着点击后你得到了更改的数据)并且片段再次作为第一次启动。通过使用 Bundle savedInstanceState 我们可以摆脱这个。如果你看一下片段的生命周期。Fragment Lifecylce你会得到一个方法“onSaveInstanceState”在片段即将被销毁时被调用。
So, we can save the state means the changed text value into that bundle like this
所以,我们可以像这样保存状态意味着改变的文本值到那个包中
int counter = 0;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("value",counter);
}
After you make the orientation the "onCreate" method will be called right? so we can just do this
确定方向后,将调用“onCreate”方法对吗?所以我们可以这样做
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
//it is the first time the fragment is being called
counter = 0;
}else{
//not the first time so we will check SavedInstanceState bundle
counter = savedInstanceState.getInt("value",0); //here zero is the default value
}
}
Now, you won't lose your value after the orientation. The modified value always will be displayed.
现在,您不会在定向后失去价值。将始终显示修改后的值。
回答by Abhi
onCreate(Bundle savedInstanceState) Function in Android:
Android 中的 onCreate(Bundle savedInstanceState) 函数:
1) When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.
1) 当 Activity 首次调用或启动时,onCreate(Bundle savedInstanceState) 方法负责创建该 Activity。
2)When ever orientation(i.e. from horizontal to vertical or vertical to horizontal) of activity gets changed or when an Activity gets forcefully terminated by any Operating System then savedInstanceState i.e. object of Bundle Class will save the state of an Activity.
2)当活动的方向(即从水平到垂直或从垂直到水平)改变或当活动被任何操作系统强行终止时,savedInstanceState 即捆绑类的对象将保存活动的状态。
3)After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState.
3) 在 Orientation 改变之后 onCreate(Bundle savedInstanceState) 将调用并重新创建活动并从savedInstanceState加载所有数据。
4)Basically Bundle class is used to stored the data of activity whenever above condition occur in app.
4)基本上Bundle类用于在应用程序中发生上述情况时存储活动数据。
5)onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.
5) 应用程序不需要 onCreate()。但是在 app 中使用它的原因是因为该方法是放置初始化代码的最佳位置。
6) You could also put your initialization code in onStart() or onResume() and when you app will load first, it will work same as in onCreate().
6) 您也可以将初始化代码放在 onStart() 或 onResume() 中,当您的应用程序首先加载时,它将与 onCreate() 中的工作方式相同。
回答by Abhi
onCreate(Bundle)
is called when the activity first starts up. You can use it to perform one-time initialization such as creating the user interface. onCreate()
takes one parameter that is either null or some state information previously saved by the onSaveInstanceState
.
onCreate(Bundle)
在活动首次启动时调用。您可以使用它来执行一次性初始化,例如创建用户界面。onCreate()
接受一个参数,该参数为 null 或之前由onSaveInstanceState
.
回答by kabilan
onCreate(Bundle savedInstanceState) gets called and savedInstanceState will be non-null if your Activity and it was terminated in a scenario(visual view) described above. Your app can then grab (catch) the data from savedInstanceState and regenerate your Activity
onCreate(Bundle savedInstanceState) 被调用并且如果您的活动在上述场景(可视化视图)中终止,则savedInstanceState 将是非空的。然后,您的应用程序可以从savedInstanceState 中抓取(捕获)数据并重新生成您的活动