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

通过以下部分代码,我们可以了解清楚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();   
    }   
}  

 

<?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,并把各个生命周期打印出来

2、运行Activity,得到如下信息

10-23 02:35:54.261: INFO/chenys(4385): onCreate-->
10-23 02:35:54.271: INFO/chenys(4385): onStart-->
10-23 02:35:54.286: INFO/chenys(4385): onResume-->

 

3、按crtl+f12切换成横屏时

10-23 02:36:58.331: INFO/chenys(4385): onSaveInstanceState-->
10-23 02:36:58.411: INFO/chenys(4385): onPause-->
10-23 02:36:58.462: INFO/chenys(4385): onStop-->
10-23 02:36:58.481: INFO/chenys(4385): onDestroy-->
10-23 02:36:58.572: INFO/chenys(4385): onCreate-->
10-23 02:36:58.622: INFO/chenys(4385): onStart-->
10-23 02:36:58.632: INFO/chenys(4385): onRestoreInstanceState-->
10-23 02:36:58.642: INFO/chenys(4385): onResume-->

 

4、再按crtl+f12切换成竖屏时,发现打印了两次相同的log

10-23 02:38:14.172: INFO/chenys(4385): onSaveInstanceState-->
10-23 02:38:14.172: INFO/chenys(4385): onPause-->
10-23 02:38:14.172: INFO/chenys(4385): onStop-->
10-23 02:38:14.172: INFO/chenys(4385): onDestroy-->
10-23 02:38:14.281: INFO/chenys(4385): onCreate-->
10-23 02:38:14.301: INFO/chenys(4385): onStart-->
10-23 02:38:14.312: INFO/chenys(4385): onRestoreInstanceState-->
10-23 02:38:14.331: INFO/chenys(4385): onResume-->
10-23 02:38:14.812: INFO/chenys(4385): onSaveInstanceState-->
10-23 02:38:14.852: INFO/chenys(4385): onPause-->
10-23 02:38:14.861: INFO/chenys(4385): onStop-->
10-23 02:38:14.892: INFO/chenys(4385): onDestroy-->
10-23 02:38:14.921: INFO/chenys(4385): onCreate-->
10-23 02:38:15.021: INFO/chenys(4385): onStart-->
10-23 02:38:15.031: INFO/chenys(4385): onRestoreInstanceState-->
10-23 02:38:15.111: INFO/chenys(4385): onResume-->

 

5、修改AndroidManifest.xml,把该Activity添加 android:configChanges="orientation",执行步骤3

10-23 02:42:32.201: INFO/chenys(4875): onSaveInstanceState-->
10-23 02:42:32.232: INFO/chenys(4875): onPause-->
10-23 02:42:32.301: INFO/chenys(4875): onStop-->
10-23 02:42:32.311: INFO/chenys(4875): onDestroy-->
10-23 02:42:32.402: INFO/chenys(4875): onCreate-->
10-23 02:42:32.471: INFO/chenys(4875): onStart-->
10-23 02:42:32.471: INFO/chenys(4875): onRestoreInstanceState-->
10-23 02:42:32.481: INFO/chenys(4875): onResume-->

 

6、再执行步骤4,发现不会再打印相同信息,但多打印了一行onConfigChanged

10-23 02:44:41.151: INFO/chenys(4875): onSaveInstanceState-->
10-23 02:44:41.151: INFO/chenys(4875): onPause-->
10-23 02:44:41.151: INFO/chenys(4875): onStop-->
10-23 02:44:41.151: INFO/chenys(4875): onDestroy-->
10-23 02:44:41.371: INFO/chenys(4875): onCreate-->
10-23 02:44:41.421: INFO/chenys(4875): onStart-->
10-23 02:44:41.521: INFO/chenys(4875): onRestoreInstanceState-->
10-23 02:44:41.541: INFO/chenys(4875): onResume-->
10-23 02:44:42.002: INFO/chenys(4875): onConfigurationChanged-->1

 

7、把步骤5的android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden",执行步骤3,就只打印onConfigChanged

10-23 02:46:43.762: INFO/chenys(5193): onConfigurationChanged-->2

 

8、执行步骤4

10-23 02:47:27.652: INFO/chenys(5193): onConfigurationChanged-->2
10-23 02:47:27.902: INFO/chenys(5193): onConfigurationChanged-->1

 

 总结:

1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android设备的幕从横切换为竖,或者从竖切换为横时,Activity生命周期会经历以下过程: 1. onPause(): 在幕旋转之前,系统会调用当前Activity的onPause()方法。这表示Activity正在失去焦点,并即将进入停止状态。 2. onSaveInstanceState(): 在幕旋转之前,系统会调用当前Activity的onSaveInstanceState()方法,用于保存Activity的状态信息。你可以在此方法中保存必要的数据,以便在Activity重新创建后进行恢复。 3. onStop(): 当幕旋转完成并且新的布局已经加载完毕后,系统会调用当前Activity的onStop()方法。此时,Activity已经完全不可见。 4. onDestroy(): 如果幕旋转导致当前Activity被销毁并重新创建,则系统会调用当前Activity的onDestroy()方法。你可以在此方法中释放资源和执行清理操作。 5. onCreate(): 在幕旋转导致Activity重新创建时,系统会调用当前Activity的onCreate()方法。你可以在此方法中重新初始化UI和恢复之前保存的数据。 6. onStart(): 在幕旋转之后,系统会调用当前Activity的onStart()方法。此时,Activity已经可见,但还没有获得焦点。 7. onResume(): 最后,系统会调用当前Activity的onResume()方法。在此方法中,你可以恢复之前的操作,并开始响应用户的交互。 需要注意的是,当幕旋转时,Activity会被销毁并重新创建,因此需要适当地处理数据保存和恢复的逻辑,以保证用户体验的连续性。可以使用onSaveInstanceState()方法保存和恢复Activity的状态信息,在onCreate()和onRestoreInstanceState()方法中进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值