通过以下部分代码,我们可以了解清楚Activity页面在横,竖屏切换时,生命周期的变化:
- public class AndroidLifecycle extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- System.out.println("First Activity =======onCreate()========");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- System.out
- .println("First Activity =======onSaveInstanceState()========");
- super.onSaveInstanceState(outState);
- }
- @Override
- protected void onRestoreInstanceState(Bundle outState) {
- System.out
- .println("First Activity =======onRestoreInstanceState()========");
- super.onRestoreInstanceState(outState);
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- System.out
- .println("First Activity =======onConfigurationChanged()========");
- super.onConfigurationChanged(newConfig);
- }
- // Called after onCreate — or after onRestart when the activity had been
- // stopped, but is now again being displayed to the user. It will be
- // followed by onResume
- protected void onStart() {
- System.out.println("First Activity =======onStart()========");
- super.onStart();
- }
- // Called after onRestoreInstanceState, onRestart, or onPause, for your
- // activity to start interacting with the user
- protected void onResume() {
- System.out.println("First Activity =======onResume()========");
- super.onResume();
- }
- // Called as part of the activity lifecycle when an activity is going into
- // the background, but has not (yet) been killed
- protected void onPause() {
- System.out.println("First Activity =======onPause()========");
- super.onPause();
- }
- // Called when you are no longer visible to the user. You will next receive
- // either onRestart, onDestroy, or nothing, depending on later user
- // activity.
- protected void onStop() {
- System.out.println("First Activity =======onStop()========");
- super.onStop();
- }
- // Perform any final cleanup before an activity is destroyed
- protected void onDestroy() {
- System.out.println("First Activity =======onDestroy()========");
- super.onDestroy();
- }
- // Called after onStop when the current activity is being re-displayed to
- // the user (the user has navigated back to it). It will be followed by
- // onStart and then onResume
- protected void onRestart() {
- System.out.println("First Activity =======onRestart()========");
- super.onRestart();
- }
- }
AndroidMenifest.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.d" android:versionCode="1" android:versionName="1.0">
- <!-- android:configChanges="orientation" -->
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".AndroidLifecycle" android:label="@string/app_name"
- android:configChanges="orientation|keyboardHidden">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
通过以上代码,我们可以知道:
1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次.
2.设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次.
3.设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法.
原帖:http://edison-cool911.iteye.com/blog/875705