标签:configchanges keyboard orientation keyboardhidden screensize
问题:横竖屏切换时Activity的生命周期?
测试环境:华为mate7
package com.virglass.beyond.activity; import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; import com.virglass.beyond.utils.LogUtil; /** * 系统设置更改 * @author Administrator * */ public class ConfigurationActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_configuration); LogUtil.e("jintuo", "==== onCreate() ===="); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); LogUtil.e("jintuo", "==== onStart() ===="); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); LogUtil.e("jintuo", "==== onRestart() ===="); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); LogUtil.e("jintuo", "==== onResume() ===="); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); LogUtil.e("jintuo", "==== onPause() ===="); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); LogUtil.e("jintuo", "==== onStop() ===="); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); LogUtil.e("jintuo", "==== onDestroy() ===="); } @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); LogUtil.e("jintuo", "==== onConfigurationChanged() ===="); } }
1、第一种情况:AndroidMainfist不设置Activity的android:configChanges时
<activity android:name="com.virglass.beyond.activity.ConfigurationActivity"> </activity>
横屏切换到竖屏Log 打印如下:
总结:AndroidMainfist不设置Activity的android:configChanges时 横屏切换竖屏/竖屏切换横屏Activity各生命周期方法只调用一次。
2、第二种情况:设置Activity的android:configChanges=”orientation”时
<activity android:name="com.virglass.beyond.activity.ConfigurationActivity" android:configChanges="orientation"> </activity>
横屏切换到竖屏Log 打印如下:
总结:竖屏切换横屏时先调用onConfigurationChanged()方法,再调用Activity各生命周期方法, 横屏切换到竖屏只掉用onConfigurationChanged()方法。
3、第三种情况:设置Activity的android:configChanges="orientation|keyboard|keyboardHidden|screenSize" 时
<activity android:name="com.virglass.beyond.activity.ConfigurationActivity" android:configChanges="orientation|keyboard|keyboardHidden|screenSize"> </activity>
横屏切换到竖屏Log 打印如下:
总结: 横屏切换到竖屏,竖屏切换到横屏各调用一次onConfigurationChanged() 方法
4、第四种情况:设置Activity的android:configChanges=”orientation|keyboard|keyboardHidden” 时 一个或多个执行结果和第二种结果相同。
5、第五种情况:在第三种情况基础上再设置Activity的android:screenOrientation="portrait" 属性,横竖屏切换无任何打印。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android Configuration横竖屏切换时Activity生命周期调用
标签:configchanges keyboard orientation keyboardhidden screensize
原文地址:http://blog.csdn.net/andylau6789/article/details/46916689