Android横竖屏切换时不销毁Activity

看个洋文:

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.

当横竖屏切换的时候会直接调用onCreate方法中的onConfigurationChanged方法,而不是重新执行onCreate方法,如果没有使用这个属性就会重新调用onCreate方法了。

AndroidManifest.xml我们通过设置

activity的android:configChanges="orientation|keyboardHidden";

属性在切换横竖屏是不再走生命周期,可是发现还是走了,问题是,在android4.0之前可以这么使用,

之后就要:

android:configChanges="orientation|keyboardHidden|navigation|screenSize";

我们有时需要横竖屏但是不销毁,可以这么处理:

/**
 * 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

我发现呢:

sensor....是仿照IOS可以双面横竖屏,根据手拿的方向感应,以前只有一面横竖屏.


【欢迎上码】

【微信公众号搜索 h2o2s2】


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值