如何让Android横竖屏切换时不销毁当前activity
2个回答
2018-07-06 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
manifest中为相应的Activity设置android:configChanges属性即可
Andorid 3.2以前的SDK可以使用如下配置
android:configChanges="orientation|keyboardHidden"
而Adnroid 3.2以后的SDK必须添加一个screenSize属性,具体如下
android:configChanges="keyboardHidden|orientation|screenSize"
或者
android:configChanges="orientation|screenSize"
对android:configChanges的总结
1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次(在三星4.0设备上切横屏和竖屏都是执行一次,而并非这里说的有执行两次的情况);
2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次;
3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。
注:上述描述是在Android3.2以前,如果缺少了keyboardHidden选项,不能防止Activity的销毁重启,也就不能执行onConfigurationChanged方法了。在3.2之后,必须加上screenSize属性才可以屏蔽调用Activity的生命周期(一些设备上可以不需要keyboardHidden,只要screenSize就可以了,保守起见还是继续保留keyboardHidden吧)。
Andorid 3.2以前的SDK可以使用如下配置
android:configChanges="orientation|keyboardHidden"
而Adnroid 3.2以后的SDK必须添加一个screenSize属性,具体如下
android:configChanges="keyboardHidden|orientation|screenSize"
或者
android:configChanges="orientation|screenSize"
对android:configChanges的总结
1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次(在三星4.0设备上切横屏和竖屏都是执行一次,而并非这里说的有执行两次的情况);
2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次;
3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。
注:上述描述是在Android3.2以前,如果缺少了keyboardHidden选项,不能防止Activity的销毁重启,也就不能执行onConfigurationChanged方法了。在3.2之后,必须加上screenSize属性才可以屏蔽调用Activity的生命周期(一些设备上可以不需要keyboardHidden,只要screenSize就可以了,保守起见还是继续保留keyboardHidden吧)。
推荐于2016-04-16 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517183
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
在编写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不被销毁呢?
------------------------------------- 背景分割线 ---------------------------------------------
资料查询:
在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了,屏幕方向改变时,应用程序的显示界面也会随着改动,而不是被销毁!
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询