开机时Android的Launcher3的Activity存在两次启动问题
前言
这个问题,类似的之前也有记录过,但由于自己记性太差,又忘了怎么分析,因此重新记录于此,方便自己查阅。
好记性不如烂笔头
正文
问题
Android P机器开机启动后,发现Launcher3的Activity(也就是Launcher)启动了两次。
日志
只截取了部分且重要的日志
ActivityManager( 1975): Config changes=200 {1.0 ?mcc?mnc [zh_CN] ldltr sw600dp w1024dp h436dp 160dpi lrg land car -touch -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 0, 0) mAppBounds=Rect(0, 0 - 1024, 504) mWindowingMode=fullscreen mActivityType=undefined} s.4}
ActivityManager( 1975): Override config changes=200 {1.0 ?mcc?mnc [zh_CN] ldltr sw600dp w1024dp h436dp 160dpi lrg land car -touch -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1024, 600) mAppBounds=Rect(0, 0 - 1024, 504) mWindowingMode=fullscreen mActivityType=undefined} s.4} for displayId=0
MyLauncher( 2509): onStart:
MyLauncher( 2509): onResume :
ActivityManager( 1975): Config changes=8 {1.0 ?mcc?mnc [zh_CN] ldltr sw600dp w1024dp h436dp 160dpi lrg land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 0, 0) mAppBounds=Rect(0, 0 - 1024, 504) mWindowingMode=fullscreen mActivityType=undefined} s.5}
ActivityManager( 1975): Override config changes=8 {1.0 ?mcc?mnc [zh_CN] ldltr sw600dp w1024dp h436dp 160dpi lrg land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1024, 504) mAppBounds=Rect(0, 0 - 1024, 504) mWindowingMode=fullscreen mActivityType=undefined} s.5} for displayId=0
MyLauncher( 2509): onPause :
MyLauncher( 2509): onStop :
MyLauncher( 2509): onDestroy :
MyLauncher( 2509): onStart:
MyLauncher( 2509): onResume :
从上面可以看出,MyLauncher启动了两次。
PS:MyLauncher是配置成Launcher的,方便测试。
原因
看下面一段代码,其实问题原因很清晰了。
ActivityManager( 1975): Config changes=8 {1.0 ?mcc?mnc [zh_CN] ldltr sw600dp w1024dp h436dp 160dpi lrg land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 0, 0) mAppBounds=Rect(0, 0 - 1024, 504) mWindowingMode=fullscreen mActivityType=undefined} s.5}
ActivityManager( 1975): Override config changes=8 {1.0 ?mcc?mnc [zh_CN] ldltr sw600dp w1024dp h436dp 160dpi lrg land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1024, 504) mAppBounds=Rect(0, 0 - 1024, 504) mWindowingMode=fullscreen mActivityType=undefined} s.5} for displayId=0
MyLauncher( 2509): onPause :
MyLauncher( 2509): onStop :
MyLauncher( 2509): onDestroy :
MyLauncher( 2509): onStart:
MyLauncher( 2509): onResume :
也即是说[Config changes=8]的改变,导致MyLauncher的Activity重新启动了。
对的,由于设备环境的变化,导致Activity重新走了一次生命周期。
上面的需要配置一下:
android:configChanges="touchscreen"
意思是当touchscreen变化时,Activity不再重启。
PS: 可访问Android官方文档:https://developer.android.google.cn/reference/android/content/pm/ActivityInfo#CONFIG_TOUCHSCREEN
解决方式
在AndroidManifest.xml中的Activity中配置上面[touchscreen]的变化。
注意,一定是Activity咯,否是无效!
下面的是Launcher3中的配置和多加了一个[touchscreen]
android:configChanges="keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenSize|screenLayout|smallestScreenSize|touchscreen"
配置好上面的属性,就可以在onConfigurationChanged中监听,看看哪个属性发声了变化。
下面内容摘抄,我懒得写了。
@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);
}
参考文章
- 《Android关于Launcher的Activity启动两次的问题调查》
- 《android:configChanges属性总结》
- 《Android中横竖屏切换时,onConfigurationChanged方法不被调用》
- 《Android开发文档》
相关文章
暂无评论...