AppCompat MODE_NIGHT_AUTO 不工作
AppCompat MODE_NIGHT_AUTO not working
AppCompatDelegate.MODE_NIGHT_AUTO 没有更新我现有的 activity,我不确定为什么。
我动态允许用户更改夜间模式。如果用户将模式更改为自动,我将设置默认的夜间模式,然后重新创建 activity:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
recreate();
如果我更改为 MODE_NIGHT_YES 或 MODE_NIGHT_NO,它会按预期工作。如果我更改为 MODE_NIGHT_AUTO,它会转到正确的 dark/light 主题,但是在从白天过渡到夜晚后它无法更新 activity。测试这个有点糟糕,因为我必须等待 sunrise/sunset(编辑:显然我可以手动更改设备上的时间而不必等待......只要不使用位置权限)。
我是否必须手动检查 onresume 中的夜间模式标志并手动更新现有活动的资源,还是我做错了什么?如果我旋转设备并且 activity 在日落之后重新创建,那么深色主题会被正确拾取,但在旋转之前它仍然会显示浅色主题。
支持库 23.4.0,Android 版本 6.0。
万一其他人想知道我做了什么来解决这个问题(尽管不确定这是否是正确的方法):
private int mCurrentNightMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
mCurrentNightMode = getCurrentNightMode();
}
@Override
protected void onPostResume() {
super.onPostResume();
if (hasNightModeChanged()) {
delayedRecreate();
}
}
private void delayedRecreate() {
Handler handler = new Handler();
handler.postDelayed(this::recreate, 1);
}
private boolean hasNightModeChanged() {
getDelegate().applyDayNight();
return mCurrentNightMode != getCurrentNightMode();
}
private int getCurrentNightMode() {
return getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
}
已在 AppCompat 24.2.0 中修复。 Revision history 将此列为 24.2.0 的 "behaviour change":
If you use the appcompat library's day/night functionality, the system
now automatically recreates your activity whenever the day/night mode
changes (either because of the time of day, or because of a call to
AppCompatDelegate.setLocalNightMode()).
AppCompatDelegate.MODE_NIGHT_AUTO 没有更新我现有的 activity,我不确定为什么。
我动态允许用户更改夜间模式。如果用户将模式更改为自动,我将设置默认的夜间模式,然后重新创建 activity:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
recreate();
如果我更改为 MODE_NIGHT_YES 或 MODE_NIGHT_NO,它会按预期工作。如果我更改为 MODE_NIGHT_AUTO,它会转到正确的 dark/light 主题,但是在从白天过渡到夜晚后它无法更新 activity。测试这个有点糟糕,因为我必须等待 sunrise/sunset(编辑:显然我可以手动更改设备上的时间而不必等待......只要不使用位置权限)。
我是否必须手动检查 onresume 中的夜间模式标志并手动更新现有活动的资源,还是我做错了什么?如果我旋转设备并且 activity 在日落之后重新创建,那么深色主题会被正确拾取,但在旋转之前它仍然会显示浅色主题。
支持库 23.4.0,Android 版本 6.0。
万一其他人想知道我做了什么来解决这个问题(尽管不确定这是否是正确的方法):
private int mCurrentNightMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
mCurrentNightMode = getCurrentNightMode();
}
@Override
protected void onPostResume() {
super.onPostResume();
if (hasNightModeChanged()) {
delayedRecreate();
}
}
private void delayedRecreate() {
Handler handler = new Handler();
handler.postDelayed(this::recreate, 1);
}
private boolean hasNightModeChanged() {
getDelegate().applyDayNight();
return mCurrentNightMode != getCurrentNightMode();
}
private int getCurrentNightMode() {
return getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
}
已在 AppCompat 24.2.0 中修复。 Revision history 将此列为 24.2.0 的 "behaviour change":
If you use the appcompat library's day/night functionality, the system now automatically recreates your activity whenever the day/night mode changes (either because of the time of day, or because of a call to AppCompatDelegate.setLocalNightMode()).