1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android:解决华为手机隐藏虚拟按键Activity被重新创建的问题

Android:解决华为手机隐藏虚拟按键Activity被重新创建的问题

时间:2024-05-29 05:55:05

相关推荐

Android:解决华为手机隐藏虚拟按键Activity被重新创建的问题

解决华为手机隐藏虚拟按键Activity被重新创建的问题

问题描述解决方法分析

问题描述

在华为手机P9上 屏幕底部虚拟按键用户可以随时隐藏或显示,在改变后 返回上一页,会导致页面重新创建,页面操作出现问题。

解决方法

在AndroidManifest.xml中出问题的activity 增加android:configChanges=“screenLayout”,这样页面就不会重建。

例如:

<activityandroid:name=".MainActivity"android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"android:screenOrientation="portrait"/>

分析

虚拟按键可隐藏 这是华为定制的功能,有个功能的手机应该都会有这个问题,导致页面重建其实就类似于手机翻转屏幕,翻转屏幕后页面会被重建,一般我们会重写Activity.onRetainNonConfigurationInstance(),在用户横竖屏切换前保存数据,在onCreate()函数中调用getLastNonConfigurationInstance(),获取onRetainNonConfigurationInstance()保存的数据恢复之前的页面。

为了避免页面被重建 我们可以使用configChanges来屏蔽掉导致发生改变的系统配置属性,怎么知道属性名是什么呢?

可以在会被重建的页面onCreate方法中将配置信息打印一遍

@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);Configuration configuration = getResources().getConfiguration();LogUtils.e("configuration:"+MGson.getInstance().toJson(configuration));}

然后在onConfigurationChanged方法中再打印一遍

@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);Configuration configuration = getResources().getConfiguration();LogUtils.e("configuration:"+MGson.getInstance().toJson(configuration));}

下面是隐藏虚拟按键返回上一页,两次打印的数据:

{"appBounds":{"bottom":"1920","left":"0","right":"1080","top":"0"},"assetsSeq":"0","colorMode":"5","compatScreenHeightDp":"547","compatScreenWidthDp":"320","compatSmallestScreenWidthDp":"320","densityDpi":"480","extraConfig":{"hwtheme":"0","isClearCache":"0","simpleuiMode":"1","userId":"0"},"fontScale":"1.0","hardKeyboardHidden":"2","keyboard":"1","keyboardHidden":"1","locale":"zh_CN_#Hans","mLocaleList":{"mList":["zh_CN_#Hans"],"mStringRepresentation":"zh-Hans-CN"},"mcc":"0","mnc":"0","navigation":"1","navigationHidden":"2","nonFullScreen":"0","orientation":"1","screenHeightDp":"616","screenLayout":"268435810","screenWidthDp":"360","seq":"76","smallestScreenWidthDp":"360","touchscreen":"3","uiMode":"17","userSetLocale":false}

{"appBounds":{"bottom":"1792","left":"0","right":"1080","top":"0"},"assetsSeq":"0","colorMode":"5","compatScreenHeightDp":"509","compatScreenWidthDp":"320","compatSmallestScreenWidthDp":"320","densityDpi":"480","extraConfig":{"hwtheme":"0","isClearCache":"0","simpleuiMode":"1","userId":"0"},"fontScale":"1.0","hardKeyboardHidden":"2","keyboard":"1","keyboardHidden":"1","locale":"zh_CN_#Hans","mLocaleList":{"mList":["zh_CN_#Hans"],"mStringRepresentation":"zh-Hans-CN"},"mcc":"0","mnc":"0","navigation":"1","navigationHidden":"2","nonFullScreen":"0","orientation":"1","screenHeightDp":"573","screenLayout":"268435794","screenWidthDp":"360","seq":"77","smallestScreenWidthDp":"360","touchscreen":"3","uiMode":"17","userSetLocale":false}

对比下可以发现screenLayout属性发生了改变,这就是我们屏蔽掉的属性名。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。