What is a OnCreate method in android

家住魔仙堡 提交于 2019-12-17 15:29:19

问题


I am new to android trying to understand what the below method does

public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters); 
}

My research ::

  • onCreate is used to start an activity
  • super is used to call the parent class constructor
  • setContentView is used to set the xml

But what is this all together -

  • onCreate(Bundle savedInstanceState) .... why did that bundle come there, what is it
  • What is that super.onCreate(savedInstanceState);

some explanation in layman terms would be helpful


回答1:


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). If the orientation changes(i.e rotating your device from landscape mode to portrait and vice versa), the activity is recreated and onCreate() method is called again, so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

For further information http://developer.android.com/guide/topics/resources/runtime-changes.html




回答2:


Bundle is used to save & recover state information for your activity. In instances like orientation changes or killing of your app or any other scenario that leads to calling of onCreate() again, the savedInstanceState bundle can be used to reload the previous state information. Familiarity with this article about Activity lifecycle will help.




回答3:


onCreate(Bundle) is where you initialize your activity. When Activity is started and application is not loaded, then both onCreate() methods will be called.

But for subsequent starts of Activity, the onCreate() of application will not be called.




回答4:


super is used to call the parent class constructor

super.onCreate(savedInstanceState); calls the onCreate() method, not the constructor, of the superclass.




回答5:


Since the onCreate method is overridden, the super keyword is used to call the onCreate method of the base class. I think




回答6:


First super.onCreate(savedInstanceState); calls the method in the superclass and saved InstanceState of the activity if any thing damage the activity so its saved in instanceState so when reload the activity it will be the same before.



来源:https://stackoverflow.com/questions/19538976/what-is-a-oncreate-method-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!