背景介绍:
在编写android小应用的时候,碰到了这样的一个问题:当推开手机的实体键盘时,屏幕由竖屏转换为横屏,此时应用程序的显示界面(Activity)就会被销毁了,这个让人比较郁闷。
如何才能让这个activity不被销毁呢?
------------------------------------- 背景分割线 ---------------------------------------------
资料查询:
在android开发网上有这么几段话:
If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.
Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).
In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.
To declare that your Activity handles a configuration change, edit the appropriate <activity> element in your manifest file to include the android:configChanges attribute with a string value that represents the configuration that you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are orientation to handle when the screen orientation changes and keyboardHidden to handle when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe character ("|").
For example, the following manifest snippet declares an Activity that handles both the screen orientation change and keyboard availability change:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
Now when one of these configurations change, MyActivity is not restarted. Instead, the Activity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your Activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your Activity.
------------------------------------ 分割线 ----------------------------------------------
解决办法:
通过上面资料的阅读,解决办法就很简单了。
首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"属性
<activity android:name=".FileBrowser" 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>
加入这条属性的含义是,应用程序将会处理屏幕方向和键盘状态(推出或合上)信息的改动。但对于其他的设备配置信息的改动则会由Android系统来处理(销毁当前Activity,然后重启一个新的Activity实例)。 那么,现在还需要在java代码的activity子类中加入配置信息改动的处理代码。这个也很简单/** * onConfigurationChanged * the package:android.content.res.Configuration. * @param newConfig, The new device configuration. * 当设备配置信息有改动(比如屏幕方向的改变,实体键盘的推开或合上等)时, * 并且如果此时有activity正在运行,系统会调用这个函数。 * 注意:onConfigurationChanged只会监测应用程序在AnroidMainifest.xml中通过 * android:configChanges="xxxx"指定的配置类型的改动; * 而对于其他配置的更改,则系统会onDestroy()当前Activity,然后重启一个新的Activity实例。 */ @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // 检测屏幕的方向:纵向或横向 if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { //当前为横屏, 在此处添加额外的处理代码 } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { //当前为竖屏, 在此处添加额外的处理代码 } //检测实体键盘的状态:推出或者合上 if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO){ //实体键盘处于推出状态,在此处添加额外的处理代码 } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES){ //实体键盘处于合上状态,在此处添加额外的处理代码 } }
别忘了在java文件中加上import android.content.res.Configuration。 这样就OK了,屏幕方向改变时,应用程序的显示界面也会随着改动,而不是被销毁! -----------------------------------还是分割线--------------------------------------------- 扩展补充: Activity中还有一属性和屏幕方向有关: <activity . . . android:screenOrientation=["unspecified" | "user" | "behind" | "landscape" | "portrait" | "sensor" | "nosensor"] . . . </activity> 比如,在Mainifest.xml的Activity元素中增加这么一个属性: android:screenOrientation="portrait" 则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。 android:screenOrientation="landscape",为横屏显示。 这里提一个小知识,Anroid模拟器中,快捷键"ctrl+F11"可以实现转屏。
//------ 背景
在Android中 如果发生了横竖屏的切换 在默认的设置下是十分悲剧的 因为他会销毁你的Activity 再重新调用onCreate方法生成这个Activity 若是我们有动画在播放 就会重新播放 如果我们有表格正在填写 就必须要重填 因而 屏蔽横竖屏切换或者防止Activity的销毁就显得格外重要
在网上看了太多帖子 都不是很清楚嘛 折腾了我一个晚上才研究明白 发篇日志以分享以备忘
//------ 第一步:获得许可
需要在中添加相应许可<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
//------ 第二步:根据不同的目标,针对Activity进行设置
目标1:屏蔽横竖屏的切换
步骤:为Activity设置一个默认的屏幕方向 方法如下:
在AndroidManifest.xml中找到该Activity 添加代码:
<activity
android:name=".ActivityName"
android:screenOrientation="landscape"
>
设置Activity的默认方向为“横向”
此处的screenOrientation有如下选项:
==================================================================
= unspecified 默认值,由系统判断状态自动切换
= landscape 横屏
= portrait 竖屏
= user 用户当前设置的orientation值
= behind 下一个要显示的Activity的orientation值
= sensor 使用传感器 传感器的方向
= nosensor 不使用传感器 基本等同于unspecified
==================================================================
目标2:防止Activity的销毁
步骤:为Activity设置configChanges属性
在AndroidManifest.xml中找到该Activity 添加代码:
<activity
android:name=".ActivityName"
android:configChanges="orientation|keyboardHidden"
>
此处的configChanges有如下选项:
==================================================================
= orientation 屏幕在纵向和横向间旋转
= keyboardHidden 键盘显示或隐藏
= fontScale 用户变更了首选的字体大小
= locale 用户选择了不同的语言设定
= keyboard 键盘类型变更,例如手机从12键盘切换到全键盘
= touchscreen或navigation 键盘或导航方式变化,一般不会发生这样的事件
==================================================================
如果需要多个选项 用"|"隔开
此处注意:如果是在实体机上测试横竖屏切换 需要orientation选项
【重点!】如果要使得程序可以在Android模拟器上测试 需要写orientation|keyboardHidden
如果缺少了keyboardHidden选项 不能防止Activity的销毁
并且在之后提到的onConfigurationChanged事件中 只能捕获竖屏变横屏的事件 不能捕获横屏变竖屏
目标3:捕获横竖屏切换的事件
步骤:在Activity中(ActivityName.java)重写onConfigurationChanged事件
@Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); switch (newConfig.orientation) { //更改为LANDSCAPE case (Configuration.ORIENTATION_LANDSCAPE): //如果转换为横向屏时,有要做的事,请写在这里 break; //更改为PORTRAIT case (Configuration.ORIENTATION_PORTRAIT): //如果转换为竖向屏时,有要做的事,请写在这里 break; } }