1,Activity生命周期
1.启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态。
2.当前Activity被其他Activity覆盖其上或被锁屏:系统会调用onPause方法,暂停当前Activity的执行(有疑问)。
3.当前Activity由被覆盖状态回到前台或解锁屏:系统会调用onResume方法,再次进入运行状态。
4.当前Activity转到新的Activity界面或按Home键回到主屏,自身退居后台:系统会先调用onPause方法,然后调用onStop方法,进入停滞状态。
5.用户后退回到此Activity:系统会先调用onRestart方法,然后调用onStart方法,最后调用onResume方法,再次进入运行状态。
6.当前Activity处于被覆盖状态或者后台不可见状态,即第2步和第4步,系统内存不足,杀死当前Activity,而后用户退回当前Activity:再次调用onCreate方法、onStart方法、onResume方法,进入运行状态。
7.用户退出当前Activity:系统先调用onPause方法,然后调用onStop方法,最后调用onDestory方法,结束当前Activity。
另外下面这三个方法也很重要
/** * Activity被系统杀死时被调用. * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死. * 另外,当跳转到其他Activity或者按Home键回到主屏时该方法也会被调用,系统是为了保存当前View组件的状态. * 在onPause之后被调用. */ @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("param", param); Log.i(TAG, "onSaveInstanceState called. put param: " + param); super.onSaveInstanceState(outState); } /** * Activity被系统杀死后再重建时被调用. * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死,用户又启动该Activity. * 这两种情况下onRestoreInstanceState都会被调用,在onStart之后. */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { param = savedInstanceState.getInt("param"); Log.i(TAG, "onRestoreInstanceState called. get param: " + param); super.onRestoreInstanceState(savedInstanceState); }
onSaveInstanceState:在一下三种情况下会被调用:在onPause之后
1,屏幕旋转,activity销毁重建
2,Activity处于后台由于系统内存不足被销毁,切记 跟杀进行不一样
3,跳转到其他acticity、home、锁屏
在很多时候我们想知道xml中控件的高度,但是在onCreate、onResume中都无法获得。这是因为Window对象还没有创建完成(那window是什么时候创建的,这个以后要参考源码分析)。//Activity窗口获得或失去焦点时被调用,在onResume之后或onPause之后 /*@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.i(TAG, "onWindowFocusChanged called."); }*/
现在告诉你,在onWindowFocesChanged可以获得到view的尺寸大小。
旋转屏幕为什么会杀死、并重建activity呢?
因为这个android的规定,屏幕旋转造成activity的config变化,config变化,activity就要被destroy。
咱manifest配置config某些参数,当这些参数变化,不会走生命周期。
onConfigurationChanged
方法,方便自己控制
Configuration Changes
If the configuration of the device (as defined by the
Resources.Configuration
class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of
onPause()
,onStop()
, andonDestroy()
as appropriate. If the activity had been in the foreground or visible to the user, onceonDestroy()
is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated fromonSaveInstanceState(Bundle)
.@Override public void onConfigurationChanged(Configuration newConfig) { Log.d(TAG, "onConfigurationChanged"); super.onConfigurationChanged(newConfig); }<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>android:configChanges="orientation|screenSize"></activity>