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

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

问题描述

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

解决方法

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

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

分析

虚拟按键可隐藏 这是华为定制的功能,有个功能的手机应该都会有这个问题,导致页面重建其实就类似于手机翻转屏幕,翻转屏幕后页面会被重建,一般我们会重写Activity.onRetainNonConfigurationInstance(),在用户横竖屏切换前保存数据,在onCreate()函数中调用getLastNonConfigurationInstance(),获取onRetainNonConfigurationInstance()保存的数据恢复之前的页面。
为了避免页面被重建 我们可以使用configChanges来屏蔽掉导致发生改变的系统配置属性,怎么知道属性名是什么呢?
可以在会被重建的页面onCreate方法中将配置信息打印一遍

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

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

    @Override
    public 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属性发生了改变,这就是我们屏蔽掉的属性名。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两块三刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值