【问题标题】:Activity is recreated when screen rotation changes if-and-only-if Night Mode is ON当屏幕旋转发生变化时重新创建活动,当且仅当夜间模式打开时
【发布时间】:2019-04-25 09:33:18
【问题描述】:

我有这个ActivityAppCompatActivity 的子类):

<activity
    android:name=".ui.settings.SettingsActivity"
    android:configChanges="keyboardHidden|smallestScreenSize|orientation|screenSize|screenLayout"
    android:launchMode="singleTask"
    android:parentActivityName=".ui.main.MainActivity" />

现在,当我在正常(即“白天”)模式下旋转手机时,我接到了onConfigurationChanged 的电话,但Activity 没有重新创建。

但是,当应用程序设置为夜间模式时(通过在 Application 中调用 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) 在创建 Activity 之前)并且我然后旋转手机,我首先得到一个调用onConfigurationChanged,然后Activity 被重新创建,即使uiMode 根本没有改变!当我将 uiMode 添加到 configChanges 时,这种情况就消失了,在我的 AndroidManifest.xml 中应该被忽略,但我需要这样做似乎很奇怪。

从源代码看来:

  • onConfigurationChanged 调用 getDelegate().onConfigurationChanged(newConfig); 转到 AppCompatDelegateImpl.onConfigurationChanged(newConfig)
  • 那里,applyDayNight() 被调用
  • 那里,getNightMode 返回AppCompatDelegateImpl.MODE_NIGHT_YES,这是有道理的,因为这是我设置的。 mapNightMode 只是返回该值不变。然后updateForNightMode(AppCompatDelegateImpl.MODE_NIGHT_YES) 被调用
  • 那里,newNightMode 被正确计算为Configuration.UI_MODE_NIGHT_YES 但是 currentNightMode(从mContext.getResources().getConfiguration().uiMode 读入)在每次旋转后总是显示为Configuration.UI_MODE_NIGHT_NO,即使Activity 在夜间模式下渲染旋转之前。

那么,这里发生了什么,我可以做些什么来获得更一致的行为? (无论应用是否处于夜间模式,在屏幕旋转后重新创建(或不创建)Activity。)

【问题讨论】:

标签: android android-appcompat android-resources android-night-mode


【解决方案1】:

似乎是一个错误,将根据此issue 在 AppCompat v1.1.0 中修复。

从 AppCompat v1.1.0-alpha03 开始​​,您不再需要 configChanges 中的 uiMode-flag 作为解决方法

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多