在Android 12上,当您关闭应用程序并尝试重新启动时,可能会出现许多问题,例如应用程序进程可能仍然在后台运行,可能会出现奇怪的UI问题等。为了解决这些问题,建议按照以下步骤进行:
在您的应用程序清单文件中添加“android:stopWithTask =”true“”属性。
在您的应用程序的主活动中添加以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the flags here
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
getWindow().setDisablePreviewScreenshots(true);
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
getWindow().setAttributes(attrs);
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set the flags here
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
window.setDisablePreviewScreenshots(true)
val attrs = window.attributes
attrs.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE
window.attributes = attrs
}
}
这些代码将设置您的应用程序以正确处理Android 12中的关闭和重新启动,以确保您的应用程序在设备上始终运行良好。