Activity 中 的 onCreate

本文详细解析了Android Activity中的onCreate方法,包括两个版本的区别及用途,帮助开发者正确使用onCreate方法进行Activity初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Activity类中的onCreate方法的时候碰到一点小情况[-使用onCreate方法出错-]

Activity中有两个onCreate方法 [-这就是我出错的原因,用错了-]

两个onCreate方法简介

1、protectedvoid onCreate (BundlesavedInstanceState)

这就是我们最常讨论的onCreate方法它在Activity创建的时候调用

 

官方注解(后附有个人理解翻译):

Calledwhen the activity is starting. This is where most initialization should go:calling setContentView(int)to inflate the activity's UI, using findViewById(int)to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri,String[], String, String[], String) to retrieve cursors for databeing displayed, etc.

Youcan call finish()from within this function, in which case onDestroy() willbe immediately called without any of the rest of the activity lifecycle (onStart(),onResume(),onPause(),etc) executing.

Derived classes must callthrough to the super class's implementation of this method. If they do not, anexception will be thrown.

Parameters

savedInstanceState

If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

--------

 

当一个Activity starting的时候调用本方法。在这个方法中我们应该做一些初始化操作例如:调用setContentView(int) 加载activity的UI界面;调用findViewById(int) 用编程的方法与UI控件进行交互;调用managedQuery(android.net.Uri, String[], String,String[], String) 【这个方法还不了解,以后用到在说】;等等。

 

你可以在这个方法中调用finish() ,这样在调用其余生命周期中的方法(onStart(), onResume(), onPause(), etc)之前调用onDestroy()方法。

 

派生类比喻用super关键字调用这个方法,否则将会报错。

 

参数

Saved Instance State(保存实例状态)—如果activity在重新启动之前被关闭,这个Bundle中包含了由onSaveInstanceState(Bundle) 提供的最新的数据

                                          注:可以为空

 

2、public void onCreate (BundlesavedInstanceState, PersistableBundlepersistentState)

官方注解(后附有个人理解翻译):

 

Sameas onCreate(android.os.Bundle)but called for those activities created with the attribute persistableModeset to persistAcrossReboots.

Parameters

savedInstanceState

if the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

persistentState

if the activity is being re-initialized after previously being shut down or powered off then this Bundle contains the data it most recently supplied to outPersistentState in onSaveInstanceState(Bundle). Note: Otherwise it is null.

----------------

 

onCreate(android.os.Bundle) 相同但是在activity需要persistableMode 属性创建activity的时候调用这个方法

参数

Saved Instance State(保存实例状态)—如果activity在重新启动之前被关闭,这个Bundle中包含了由onSaveInstanceState(Bundle) 提供的最新的数据

                                          注:可以为空

Persistent State(当前状态)—如果activity在重新启动之前被关闭或者断电,这个Bundle中包含了由onSaveInstanceState(Bundle) 提供的最新的数据

                                          注:可以为空

 

 

 

参考

à  http://blog.youkuaiyun.com/lincyang/article/details/45287599

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值