在开机的时候,发现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);
}