diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 64d4d9371..c6e4f40bc 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -51,12 +51,13 @@ class App : Application() { .autoClear(false) } - fun initNightTheme() {//对整个应用生效,不要手动调用recreate - if (getPrefBoolean("isNightTheme")) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) + fun initNightTheme() { + val targetMode = if (getPrefBoolean("isNightTheme")) { + AppCompatDelegate.MODE_NIGHT_YES } else { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) + AppCompatDelegate.MODE_NIGHT_NO } + AppCompatDelegate.setDefaultNightMode(targetMode) } /** diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 2b709ec4b..89a698b54 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -40,8 +40,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar sharedPreferences ?: return when (key) { "transparentStatusBar" -> { - postEvent(Bus.RECREATE, "") - Handler().postDelayed({ activity?.recreate() }, 100) + recreateActivities() } "colorPrimary", "colorAccent", "colorBackground" -> { if (backgroundIsDark(sharedPreferences)) { @@ -104,6 +103,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar .putInt("colorBackgroundNight", App.INSTANCE.getCompatColor(R.color.md_grey_800)) .apply() App.INSTANCE.upThemeStore() + recreateActivities() } .setNegativeButton(R.string.cancel, null) .show().upTint() @@ -134,7 +134,12 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar private fun upTheme(isNightTheme: Boolean) { if (App.INSTANCE.getPrefBoolean("isNightTheme") == isNightTheme) { App.INSTANCE.upThemeStore() + recreateActivities() } } + private fun recreateActivities() { + postEvent(Bus.RECREATE, "") + Handler().postDelayed({ activity?.recreate() }, 100L) + } } \ No newline at end of file