28

我是 android 新手,试图了解以下方法的作用

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

我的研究::

  • onCreate 用于启动一个活动
  • super用于调用父类构造函数
  • setContentView 用于设置xml

但这一切是什么——

  • onCreate(Bundle savedInstanceState)....为什么那个包裹会到那里,它是什么
  • 那是什么super.onCreate(savedInstanceState);

外行的一些解释会有所帮助

4

5 回答 5

23

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

于 2013-10-23T10:37:32.150 回答
13

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.

于 2013-10-23T10:31:09.633 回答
2

如果有任何东西损坏了活动,则首先super.onCreate(savedInstanceState);调用活动中的方法superclass 并保存,因此将其保存在其中,因此当重新加载活动时,它将与之前相同。InstanceStateinstanceState

于 2019-09-05T19:00:31.143 回答
2

由于重写了 onCreate 方法,所以使用 super 关键字调用基类的 onCreate 方法。我认为

于 2019-08-19T07:16:44.983 回答
1

super用于调用父类构造函数

super.onCreate(savedInstanceState);调用onCreate()超类的方法,而不是构造函数。

于 2019-06-06T23:52:09.587 回答