在使用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 |
--------
当一个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 persistableMode
set 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 |
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 |
----------------
和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