如何让Android横竖屏切换时不销毁当前activity

本文介绍如何在Android应用中处理屏幕方向变化及键盘状态变更等问题,通过设置Activity的configChanges属性并覆盖onConfigurationChanged方法,实现应用界面的平滑过渡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(), andonDestroy() 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 andonConfigurationChanged(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 areorientation 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 toonConfigurationChanged(). 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"]
    . . .
&lt;/activity>

比如,在Mainifest.xml的Activity元素中增加这么一个属性:

android:screenOrientation="portrait"

则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。

android:screenOrientation="landscape“为横屏显示。

这里提一个小知识,Anroid模拟器中,快捷键"ctrl+F11"可以实现转屏。


内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值