本文主要是介绍Activity转屏重建之 Activity.onConfigurationChanged,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
偶尔也会遇到由于转屏引起的一些问题。
有些时候,并不希望由于转屏使得Activity取重建。
再如键盘消失后的重建。
下面以一个demo为例子,小小总结一下用法。
如果想在转屏后,屏幕上立马打印出当前处于什么横竖屏状态
1.都知道有个属性android:configChanges可以用来定义什么情况下可以使得Activity不会restart。
android:configChanges="screenSize|orientation"
2.下面再来看看用法
首先是AndroidManifest.xml,有两个点需要注意,1是添加权限,2是添加在哪些因素改变后希望不被重建。
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission> <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="screenSize|orientation" android:theme="@style/AppTheme.NoActionBar">
其次是,需要复写Activity的onConfigurationChanged方法,假如在横竖屏切换后,需要改变的配置。
@Override public void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig); Log.d(TAG_, "onConfigurationChanged --"); int orientation = getResources().getConfiguration().orientation; mainContentTxv.setText("current orientaion is " + orientation); // super.onConfigurationChanged(newConfig); }
备注:
起始,只加了orientation,发现并不能达到转屏后实时屏幕显示横竖屏信息。
原因是,当转屏后,可供Activity使用的screenSize也发生了变化,如果只限定为orientation,并不能达到效果。这个最小变化的子集,需要关注。
3.Activity生命周期
当加了上述限制后,Activity在发生转屏后,将只调用onConfigurationChanged方法继续运行,其他回调方法不会发生。
在每个回调中打印相应的log,验证了这一点,结果如下:
01-07 09:21:25.173 23427 23427 D K2K : onCreate -- 01-07 09:21:25.225 23427 23427 D K2K : onStart -- 01-07 09:21:25.228 23427 23427 D K2K : onResume -- //横屏 01-07 09:21:36.687 23427 23427 D K2K : onConfigurationChanged -- //竖屏 01-07 09:21:40.112 23427 23427 D K2K : onConfigurationChanged -- //横屏 01-07 09:21:43.635 23427 23427 D K2K : onConfigurationChanged -- //返回launcher//back 01-07 09:21:47.134 23427 23427 D K2K : onPause -- 01-07 09:21:48.233 23427 23427 D K2K : onStop -- 01-07 09:21:48.234 23427 23427 D K2K : onDestory --
附录:
Android API原文描述:
Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will be restarted if any of these configuration changes happen in the system. Otherwise, the activity will remain running and itsActivity.onConfigurationChanged
method called with the new configuration.
Note that all of these configuration changes can impact the resource values seen by the application, so you will generally need to re-retrieve all resources (including view layouts, drawables, etc) to correctly handle any configuration change.
These values must be kept in sync with those in ActivityInfo
and include/utils/ResourceTypes.h.
Must be one or more (separated by '|') of the following constant values.
Constant | Value | Description |
---|---|---|
mcc | 0x0001 | The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code. |
mnc | 0x0002 | The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code. |
locale | 0x0004 | The locale has changed, that is the user has selected a new language that text should be displayed in. |
touchscreen | 0x0008 | The touchscreen has changed. Should never normally happen. |
keyboard | 0x0010 | The keyboard type has changed, for example the user has plugged in an external keyboard. |
keyboardHidden | 0x0020 | The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation. |
navigation | 0x0040 | The navigation type has changed. Should never normally happen. |
orientation | 0x0080 | The screen orientation has changed, that is the user has rotated the device. |
screenLayout | 0x0100 | The screen layout has changed. This might be caused by a different display being activated. |
uiMode | 0x0200 | The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc. |
screenSize | 0x0400 | The current available screen size has changed. If applications don't target at leastHONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in the currently available size, so will change when the user switches between landscape and portrait. |
smallestScreenSize | 0x0800 | The physical screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display. |
layoutDirection | 0x2000 | The layout direction has changed. For example going from LTR to RTL. |
fontScale | 0x40000000 | The font scaling factor has changed, that is the user has selected a new global font size. |
这篇关于Activity转屏重建之 Activity.onConfigurationChanged的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!