免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1215 | 回复: 0

横竖屏切换时候 Activity 的生命周期 [复制链接]

论坛徽章:
0
发表于 2011-12-23 03:03 |显示全部楼层

一、Activity 的 ConfigChanges 属性

官方解释: 通过设置这个属性可以使Activity捕捉设备状态变化,以下是可以被识别的内容:


CONFIG_FONT_SCALE
CONFIG_MCC
CONFIG_MNC
CONFIG_LOCALE
CONFIG_TOUCHSCREEN
CONFIG_KEYBOARD
CONFIG_NAVIGATION
CONFIG_ORIENTATION


设置方法:将下列字段用“|”符号分隔开,例如:“locale|navigation|orientation”

Value
Description

“mcc“
The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家。

“mnc“
The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移动网号,在一个国家或者地区中,用于区分手机用户的服务商。

“locale“
The locale has changed — for example, the user has selected a new language that text should be displayed in.用户所在地区发生变化。

“touchscreen“
The touchscreen has changed. (This should never normally happen.)

“keyboard“
The keyboard type has changed — for example, the user has plugged in an external keyboard.键盘模式发生变化,例如:用户接入外部键盘输入。

“keyboardHidden“
The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.用户打开手机硬件键盘

“navigation“
The navigation type has changed. (This should never normally happen.)

“orientation“
The screen orientation has changed — that is, the user has rotated the device.设备旋转,横向显示和竖向显示模式切换。

“fontScale“
The font scaling factor has changed — that is, the user has selected a new global font size.全局字体大小缩放发生改变

 

项目的AndroidManifest.xml文件配置方法:

01<manifest xmlns:android="http://schemas.android.com/apk/res/android"
02package="com.androidres.ConfigChangedTesting"
03android:versionCode="1"
04android:versionName="1.0.0">
05<application android:icon="@drawable/icon" android:label="@string/app_name">
06<activity android:name=".ConfigChangedTesting"
07android:label="@string/app_name"
08android:configChanges="keyboardHidden|orientation">
09<intent-filter>
10<action android:name="android.intent.action.MAIN" />
11<category android:name="android.intent.category.LAUNCHER" />
12</intent-filter>
13</activity>
14</application>
15</manifest>

 

二、横竖屏切换时候 Activity 的生命周期

测试代码:

 

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

 

Activity属性配置:

 

01<application android:icon="@drawable/icon" android:label="@string/app_name">
02        <activity android:name=".AndroidLifecycle"
03                  android:label="@string/app_name"
04                  android:configChanges="orientation|keyboardHidden">
05            <intent-filter>
06                <action android:name="android.intent.action.MAIN" />
07                <category android:name="android.intent.category.LAUNCHER" />
08            </intent-filter>
09        </activity>
10 
11    </application>

 

在 Android 2.2 模拟器上测试结果为:

1、不设置Activity的android:configChanges时:

Simulator_切换到横屏Simulator_切换到横屏

 

Simulator_切换到竖屏Simulator_切换到竖屏

 

模拟器这种情况切屏会重新调用各个生命周期,切横屏时会生命周期执行一次,切竖屏时生命周期会执行两次,且横竖屏onConfigurationChanged未被调用但是在HTC DESIRE HD(G10) 这款手机上测试结果为:生命周期都只执行一次,且横竖屏onConfigurationChanged未被调用。

 

2、设置Activity的android:configChanges="orientation"时:

Simulator_orientation_切换到横屏Simulator_orientation_切换到横屏

 

Simulator_orientation_切换到竖屏Simulator_orientation_切换到竖屏

 

模拟器这种情况切屏还是会重新调用各个生命周期,切横屏onConfigurationChanged未执行、竖屏时onConfigurationChanged执行1次。但是在HTC DESIRE HD(G10) 这款手机上测试结果为:生命周期未发生变化,且横竖屏onConfigurationChanged都只执行一次。

 

3、设置Activity的android:configChanges="orientation|keyboardHidden"时Simulator_orientation_keyboardHidden_切换到横屏Simulator_orientation_keyboardHidden_切换到横屏

 

Simulator_orientation_keyboardHidden_切换到竖屏Simulator_orientation_keyboardHidden_切换到竖屏

 

模拟器这种情况切屏不会重新调用各个生命周期,切横屏时onConfigurationChanged会执行1次,切竖屏时onConfigurationChanged方法调用2次。 但是在HTC DESIRE HD(G10) 这款手机上测试结果为:横竖屏onConfigurationChanged都只执行一次。

 

结论:可以看出在G10这款真机上测试的结果比模拟器更加合理。

微软企业开发技术 | 移动开发(Google Android、Windows Mobile)技术| 嵌入式系统设计与开发 | JAVA开发
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP