横竖屏切换时候activity的生命周期

通过以下部分代码,我们可以了解清楚Activity页面在横,竖屏切换时,生命周期的变化: 

Java代码   收藏代码
  1. public class AndroidLifecycle extends Activity {  
  2.   
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         System.out.println("First Activity =======onCreate()========");  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.     }  
  8.   
  9.     @Override  
  10.     protected void onSaveInstanceState(Bundle outState) {  
  11.         System.out  
  12.                 .println("First Activity =======onSaveInstanceState()========");  
  13.         super.onSaveInstanceState(outState);  
  14.     }  
  15.   
  16.     @Override  
  17.     protected void onRestoreInstanceState(Bundle outState) {  
  18.         System.out  
  19.                 .println("First Activity =======onRestoreInstanceState()========");  
  20.         super.onRestoreInstanceState(outState);  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onConfigurationChanged(Configuration newConfig) {  
  25.         System.out  
  26.                 .println("First Activity =======onConfigurationChanged()========");  
  27.         super.onConfigurationChanged(newConfig);  
  28.     }  
  29.   
  30.     // Called after onCreate — or after onRestart when the activity had been  
  31.     // stopped, but is now again being displayed to the user. It will be  
  32.     // followed by onResume  
  33.     protected void onStart() {  
  34.         System.out.println("First Activity =======onStart()========");  
  35.         super.onStart();  
  36.     }  
  37.   
  38.     // Called after onRestoreInstanceState, onRestart, or onPause, for your  
  39.     // activity to start interacting with the user  
  40.     protected void onResume() {  
  41.         System.out.println("First Activity =======onResume()========");  
  42.         super.onResume();  
  43.     }  
  44.   
  45.     // Called as part of the activity lifecycle when an activity is going into  
  46.     // the background, but has not (yet) been killed  
  47.     protected void onPause() {  
  48.         System.out.println("First Activity =======onPause()========");  
  49.         super.onPause();  
  50.     }  
  51.   
  52.     // Called when you are no longer visible to the user. You will next receive  
  53.     // either onRestart, onDestroy, or nothing, depending on later user  
  54.     // activity.  
  55.     protected void onStop() {  
  56.         System.out.println("First Activity =======onStop()========");  
  57.         super.onStop();  
  58.     }  
  59.   
  60.     // Perform any final cleanup before an activity is destroyed  
  61.     protected void onDestroy() {  
  62.         System.out.println("First Activity =======onDestroy()========");  
  63.         super.onDestroy();  
  64.     }  
  65.   
  66.     // Called after onStop when the current activity is being re-displayed to  
  67.     // the user (the user has navigated back to it). It will be followed by  
  68.     // onStart and then onResume  
  69.     protected void onRestart() {  
  70.         System.out.println("First Activity =======onRestart()========");  
  71.         super.onRestart();  
  72.     }  
  73. }  


AndroidMenifest.xml文件: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.d" android:versionCode="1" android:versionName="1.0">  
  4.     <!-- android:configChanges="orientation" -->  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:name=".AndroidLifecycle" android:label="@string/app_name"  
  7.             android:configChanges="orientation|keyboardHidden">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN" />  
  10.                 <category android:name="android.intent.category.LAUNCHER" />  
  11.             </intent-filter>  
  12.         </activity>  
  13.     </application>  
  14. </manifest>   



通过以上代码,我们可以知道: 
1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次. 

2.设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次. 

3.设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法.

 

原帖:http://edison-cool911.iteye.com/blog/875705

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值