Android关于Launcher的Activity启动两次的问题调查

在开机的时候,发现launcher的Activity启动了两次。

经过开机打印Log发现:
2019-08-05 16:37:31.891 1754-1800/system_process W/WindowManager: Force clearing freeze: AppWindowToken{f258030 token=Token{59f4973 ActivityRecord{ad731e2 u0 com.ad.launcher/.LauncherActivity t25}}}

发现Launcher被停止掉了,然后重新启动。

经过调查发现,安卓机制,当Configuration配置发生变化是,Launcher会重新启动。详细参考官网说明:
https://developer.android.com/reference/android/content/res/Configuration
https://developer.android.com/guide/topics/manifest/activity-element

根据文档:
在这里插入图片描述
所以,只要AndroidManifest.xml中的Activity增加属性android:configChanges就可以保持Activity运行,不用重新启动Activity。

那么我的项目中到底哪个Configuration发生了变化呢?

继续查看卡机打印Log,发现:
2019-08-05 16:37:28.043 1754-1848/system_process I/ActivityManager: Config changes=8 {1.0 ?mcc?mnc [en_US] ldltr sw811dp w1442dp h675dp 213dpi lrg long land car night finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 0, 0) mAppBounds=Rect(0, 0 - 1920, 931) mWindowingMode=fullscreen mActivityType=undefined} s.7}
2019-08-05 16:37:28.100 1754-1848/system_process I/ActivityManager: Override config changes=8 {1.0 ?mcc?mnc [en_US] ldltr sw811dp w1442dp h675dp 213dpi lrg long land car night finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1920, 931) mAppBounds=Rect(0, 0 - 1920, 931) mWindowingMode=fullscreen mActivityType=undefined} s.7} for displayId=0

Config changes=8,根据官方文档和代码:
在这里插入图片描述

    /**
     * Bit in {@link #configChanges} that indicates that the activity
     * can itself handle changes to the touchscreen type.  Set from the
     * {@link android.R.attr#configChanges} attribute.
     */
    public static final int CONFIG_TOUCHSCREEN = 0x0008;

发现发生变化的属性是CONFIG_TOUCHSCREEN,所以在Activity属性中增加:
android:configChanges=“screenSize”

当然也可以增加:android:configChanges=“keyboardHidden|orientation|locale|screenLayout|mcc|mnc|uiMode|screenSize|layoutDirection|fontScale|smallestScreenSize|navigation|keyboard|touchscreen”

监听变化来知道哪个CONFIG发生了变化:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }

        Log.i(TAG,"config" + newConfig);
        Log.i(TAG,"config.fontScale" + newConfig.fontScale);
        Log.i(TAG,"config.colorMode" + newConfig.colorMode);
        Log.i(TAG,"config.densityDpi" + newConfig.densityDpi);
        Log.i(TAG,"config.hardKeyboardHidden" + newConfig.hardKeyboardHidden);
        Log.i(TAG,"config.keyboard" + newConfig.keyboard);
        Log.i(TAG,"config.keyboardHidden" + newConfig.keyboardHidden);
        Log.i(TAG,"config.mcc" + newConfig.mcc);
        Log.i(TAG,"config.mnc" + newConfig.mnc);
        Log.i(TAG,"config.navigation" + newConfig.navigation);
        Log.i(TAG,"config.navigationHidden" + newConfig.navigationHidden);
        Log.i(TAG,"config.orientation" + newConfig.orientation);
        Log.i(TAG,"config.screenHeightDp" + newConfig.screenHeightDp);
        Log.i(TAG,"config.screenLayout" + newConfig.screenLayout);
        Log.i(TAG,"config.uiMode" + newConfig.uiMode);
        Log.i(TAG,"config.touchscreen" + newConfig.touchscreen);
        Log.i(TAG,"config.smallestScreenWidthDp" + newConfig.smallestScreenWidthDp);
        Log.i(TAG,"config.smallestScreenWidthDp" + newConfig.touchscreen);
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sunxiaolin2016

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

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

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

打赏作者

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

抵扣说明:

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

余额充值