简体   繁体   English

什么是 onCreate(Bundle savedInstanceState)

[英]What's onCreate(Bundle savedInstanceState)

Can anyone help me to know about the Bundle savedInstanceState in onCreate(Bundle savedInstanceState) I am newbie in Android.谁能帮助我了解的Bundle savedInstanceStateonCreate(Bundle savedInstanceState)我在Android的新手。 I try to understand it from developer.android.com.我试着从 developer.android.com 理解它。 But I am not able to understand.但我无法理解。 Can anyone simplify it?任何人都可以简化它吗?

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 (eg, orientation change) so that you don't lose this prior information.如果您将应用程序的状态保存在一个包中(通常是onSaveInstanceState非持久性动态数据),如果需要重新创建活动(例如,方向更改),则可以将其传递回onCreate ,这样您就不会丢失这个先验信息。 If no data was supplied, savedInstanceState is null.如果未提供数据,则savedInstanceState为空。

... you should use the onPause() method to write any persistent data (such as user edits) to storage. ...您应该使用 onPause() 方法将任何持久数据(例如用户编辑)写入存储。 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.此外,在将活动置于这种后台状态之前调用方法 onSaveInstanceState(Bundle),允许您将活动中的任何动态实例状态保存到给定的 Bundle 中,以便稍后在 onCreate(Bundle) 中接收,如果活动需要重新创建。 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) 中保存持久数据很重要,因为后者不是生命周期回调的一部分,因此不会在其文档中描述的所有情况下都被调用。

source来源

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 null,当活动方向改变时它会被使用......

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. Android 提供了另一种优雅的方式来实现这一点。 To achieve this, we have to override a method called onSaveInstanceState() .为了实现这一点,我们必须重写一个名为onSaveInstanceState()的方法。 Android platform allows the users to save any instance state. Android 平台允许用户保存任何实例状态。 Instance state can be saved in the Bundle.实例状态可以保存在 Bundle 中。 Bundle is passed as argument to the onSaveInstanceState method. Bundle 作为参数传递给 onSaveInstanceState 方法。

we can load the saved instance state from the Bundle passed as argument to the onCreate method.我们可以从作为参数传递给onCreate方法的 Bundle 加载保存的实例状态。 We can also load the saved instance state in onRestoreInstanceState method.我们还可以在onRestoreInstanceState方法中加载保存的实例状态。 But I will leave that for the readers to figure out.但我会把它留给读者去弄清楚。

As Dhruv Gairola answered, you can save the state of the application by using Bundle savedInstanceState.正如 Dhruv Gairola 回答的那样,您可以使用 Bundle savedInstanceState 来保存应用程序的状态。 I am trying to give a very simple example that new learners like me can understand easily.我试图举一个非常简单的例子,像我这样的新学习者可以很容易地理解。

Suppose, you have a simple fragment with a TextView and a Button.假设您有一个带有 TextView 和 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.通过使用 Bundle savedInstanceState 我们可以摆脱这个。 If you take a look into the life cyle of the fragment.如果你看一下片段的生命周期。 Fragment Lifecylce you will get that a method "onSaveInstanceState" is called when the fragment is about to destroyed. 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?确定方向后,将调用“onCreate”方法对吗? so we can just do this所以我们可以这样做

@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.将始终显示修改后的值。

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.当 Activity 首次调用或启动时,onCreate(Bundle savedInstanceState) 方法负责创建该 Activity。

  2. When ever orientation(ie 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 ie object of Bundle Class will save the state of an Activity.当活动的方向(即从水平到垂直或从垂直到水平)改变或当活动被任何操作系统强行终止时,savedInstanceState 即捆绑类的对象将保存活动的状态。

  3. After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState. Orientation 更改后,onCreate(Bundle savedInstanceState) 将调用并重新创建活动并从savedInstanceState 加载所有数据。

  4. Basically Bundle class is used to stored the data of activity whenever above condition occur in app.基本上 Bundle 类用于在应用程序中发生上述情况时存储活动数据。

  5. onCreate() is not required for apps.应用程序不需要 onCreate()。 But the reason it is used in app is because that method is the best place to put initialization code.但是在 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().您也可以将初始化代码放在 onStart() 或 onResume() 中,当您的应用程序首先加载时,它的工作方式与 onCreate() 中的相同。

onCreate(Bundle) is called when the activity first starts up. onCreate(Bundle)在活动第一次启动时被调用。 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()接受一个参数,该参数为 null 或一些先前由onSaveInstanceState保存的状态信息。

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. onCreate(Bundle savedInstanceState) 被调用并且如果您的活动在上述场景(可视化视图)中终止,则savedInstanceState 将是非空的。 Your app can then grab (catch) the data from savedInstanceState and regenerate your Activity然后,您的应用程序可以从savedInstanceState 中抓取(捕获)数据并重新生成您的活动

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

相关问题 在Robolectric中使用savedInstanceState Bundle测试Fragment的onCreate方法? - Testing a Fragment's onCreate method with a savedInstanceState Bundle in Robolectric? FragmentActivity在onCreate savedInstanceState Bundle中的NullPointer - FragmentActivity NullPointer in onCreate savedInstanceState Bundle 已经定义“ onCreate(捆绑保存的InstanceState)” - “onCreate(Bundle savedInstanceState)” is already defined onCreate(Bundle savedInstanceState)始终为null - onCreate(Bundle savedInstanceState) in always null 在onCreate(Bundle savedInstanceState)中创建捆绑对象的位置在哪里 - Where is Bundle object created in onCreate(Bundle savedInstanceState) 捆绑的saveInstanceState在onCreate()上为null,尽管saveInstanceState已经设置了数据 - Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data `Bundle savedInstanceState` 的范围是什么 - What is the scope of `Bundle savedInstanceState` 执行doInBackground(AsyncTask) - onCreate(Bundle savedInstanceState) - Executing doInBackground (AsyncTask) - onCreate(Bundle savedInstanceState) 两个 oncreate(bundle savedinstancestate) 在同一个活动中? - Two oncreate(bundle savedinstancestate) in the same activity? Android kotlin onCreate(savedInstanceState: Bundle?) 导致 IllegalArgumentException - Android kotlin onCreate(savedInstanceState: Bundle?) cause IllegalArgumentException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM