`
quanminchaoren
  • 浏览: 919712 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android 屏幕切换时不销毁Activity

阅读更多

在编写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

    需要注意的是,让 Android 横竖屏切换时不销毁当前的 Activity,并不意味着我们可以完全忽视屏幕方向的改变。我们仍然需要在 Java 代码中加入配置信息改动的处理代码,以便正确处理屏幕方向的改变。 另外,Activity...

    android屏幕切换项目

    开发者需要理解Android的生命周期管理,特别是在屏幕切换时,确保Activity或Fragment的正确启动和销毁。 为了优化用户体验,我们还需要考虑一些额外的点,如使用动画平滑过渡,处理配置更改(如屏幕旋转)导致的...

    activity横竖屏切换时销毁的处理

    - **忽略配置变更**:可以通过在AndroidManifest.xml中对应的Activity标签内添加`android:configChanges="orientation|screenSize"`,声明自己处理配置变更,这样系统就不会默认销毁Activity,而是调用`...

    Android屏幕旋转时Activity的生命周期

    当屏幕旋转时,Android默认的行为是销毁当前的Activity并重建一个新的实例,这就导致了`onCreate()`、`onStart()`和`onResume()`的再次调用。这是因为Android认为屏幕旋转是一种配置更改,可能会改变Activity的布局...

    Android-这是个方便切换夜间模式的库利用官方夜间模式同时不用重启Activity

    标题提到的“Android-这是个方便切换夜间模式的库利用官方夜间模式同时不用重启Activity”是一个旨在简化Android应用夜间模式切换的第三方库。这个库允许开发者在不重新启动Activity的情况下实现夜间模式的即时切换...

    android点击按钮实现多个Activity滑动切换

    Fragment是Android中的一个组件,可以在同一个Activity中展示多个界面,这在某种程度上可以减少Activity的频繁创建和销毁,提高应用性能。但在这个特定的需求中,我们更倾向于使用自定义动画来实现Activity间的滑动...

    Android编程实现横竖屏切换时不销毁当前activity和锁定屏幕的方法

    本文实例讲述了Android编程实现横竖屏切换时不销毁当前activity和锁定屏幕的方法。分享给大家供大家参考,具体如下: 首先在Mainifest.xml的Activity元素中加入android:configChanges=”orientation|keyboardHidden...

    android屏幕的切换

    6. **处理配置更改**:在AndroidManifest.xml中,可以通过在&lt;activity&gt;标签内添加`android:configChanges="orientation|screenSize"`来防止Activity因屏幕方向改变而被销毁重建。这时,你需要重写`...

    Android-skin-sprite另一种不重启Activity切换Android夜间模式的库

    而“Android-skin-sprite”库提供了一种无需重启Activity就能实现夜间模式切换的解决方案。这个库主要用于动态改变Android应用的主题和皮肤,使得用户可以在不同光线环境下舒适地使用应用。 首先,我们来理解...

    Activity 的生命周期 以及 横屏竖屏切换时 Activity 的状态变化

    1. **android:configChanges="orientation"**:指定该`Activity`可以在屏幕方向改变时不被销毁。此时,只会调用`onConfigurationChanged(Configuration newConfig)`方法,而不会重新创建`Activity`实例。 2. **...

    滑动销毁activity

    在Android开发中,"滑动销毁activity"是一种增强用户体验的设计,它模仿了iOS系统的滑动退出手势,使得用户可以通过从屏幕边缘向内滑动来关闭当前活动(Activity)。这一功能在Android应用中并非默认实现,但通过...

    android左右滑动切换activity

    在Android开发中,"左右滑动切换Activity"是一种常见的用户界面设计,它为用户提供了一种流畅、直观的导航方式,增强了应用的用户体验。...同时,要注意优化性能,避免因频繁创建和销毁Activity导致的内存问题。

    屏幕旋转180度 activity旋转180度 源代码

    这段代码告诉系统,当屏幕方向或尺寸变化时,不要销毁Activity,而是调用`onConfigurationChanged()`方法。 2. `onConfigurationChanged()` 方法: 在Activity中重写`onConfigurationChanged()`方法,以在屏幕旋转...

    Android Activity学习笔记

    3. 销毁阶段:包括onDestroy方法,这个方法是系统自动调用的,用于销毁Activity。 多个Activity的生命周期 当我们打开多个Activity时,每个Activity的生命周期将会相互影响。例如,当我们从Activity A跳转到...

    android 不同activity切换时它们的回调函数调用逻辑的研究

    当我们从一个Activity切换到另一个Activity时,Android系统会按照特定的生命周期回调函数调用来管理这些活动。本研究主要探讨了在不同Activity之间切换时,它们的回调函数调用逻辑。 首先,我们从一个Activity启动...

    Android Activity嵌套Fragmnet实现横竖屏切换

    1. 使用`android:configChanges="orientation|screenSize"`属性,在AndroidManifest.xml中指定Activity对横竖屏变化的处理方式,让系统不再默认销毁Activity。 2. 在Activity中重写`onConfigurationChanged...

    android fragment切换防止destroy

    在Android系统中,当Activity发生配置改变(如屏幕旋转)时,默认情况下会销毁并重新创建Activity及其中的Fragment。这种行为可能导致数据丢失和用户体验中断。为了解决这个问题,我们可以利用Fragment的`hide()`和`...

    Android 8.0系统界面切换时闪屏问题

    ### Android 8.0系统界面切换时闪屏问题解析及解决方案 #### 一、问题背景与现象 在Android 8.0系统中,开发者可能会遇到一个常见问题:当应用程序进行界面切换时出现短暂的闪屏现象。这种现象不仅影响用户体验,...

    Android Activity内嵌Fragment,当Activity recreate时Fragment被添加多次,造成界面重叠

    然而,在使用Fragment时,有时会出现一个问题:当Activity由于配置更改(如屏幕旋转)等原因需要重建(recreate)时,内嵌的Fragment可能会被不正确地添加多次,导致界面重叠。这个问题通常与如何管理和恢复Fragment...

Global site tag (gtag.js) - Google Analytics