Activity转屏重建之 Activity.onConfigurationChanged

来源:互联网 发布:mac打不开word文档 编辑:程序博客网 时间:2024/06/04 01:33

偶尔也会遇到由于转屏引起的一些问题。

有些时候,并不希望由于转屏使得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//back01-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.

ConstantValueDescriptionmcc0x0001The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.mnc0x0002The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.locale0x0004The locale has changed, that is the user has selected a new language that text should be displayed in.touchscreen0x0008The touchscreen has changed. Should never normally happen.keyboard0x0010The keyboard type has changed, for example the user has plugged in an external keyboard.keyboardHidden0x0020The 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.navigation0x0040The navigation type has changed. Should never normally happen.orientation0x0080The screen orientation has changed, that is the user has rotated the device.screenLayout0x0100The screen layout has changed. This might be caused by a different display being activated.uiMode0x0200The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.screenSize0x0400The 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.smallestScreenSize0x0800The 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.layoutDirection0x2000The layout direction has changed. For example going from LTR to RTL.fontScale0x40000000The font scaling factor has changed, that is the user has selected a new global font size.

      

阅读全文
0 0
原创粉丝点击