从最近的背景中删除应用程序后,应用程序中的暗模式不适用
Dark mode in app is not applying after removing app from recent background
先看下图
[白色模式 1]https://i.stack.imgur.com/QKcju.jpg
[白色模式 2]https://i.stack.imgur.com/QKcju.jpg
[深色模式 1]https://i.stack.imgur.com/QKcju.jpg
[深色模式 2]https://i.stack.imgur.com/QKcju.jpg
问题是 -> 当我通过单击抽屉布局设置按钮打开黑暗 activity 并在按下黑暗按钮后黑暗模式应用于整个应用程序,当我从最近的背景中删除应用程序然后打开应用程序,然后白色模式自动应用于 activity 主页,但在从抽屉布局再次打开深色 activity 后,它自动应用深色模式。
这是我实现的代码
const val FIRST_START = "FirstStart"
const val NIGHT_MODE = "NightMode"
const val PREF = "AppSettingsPrefs"
class language_settings : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loadLocate() // call LoadLocate
setContentView(R.layout.activity_language_settings)
//save preferences specific to an app, like save the dark mode again when we again
open the app
val appSettingsPrefs: SharedPreferences = getSharedPreferences(PREF, 0)
val isNightModeOn: Boolean = appSettingsPrefs.getBoolean(NIGHT_MODE, false)
val isFirstStart: Boolean = appSettingsPrefs.getBoolean(FIRST_START,false)
//Create a new Editor for these preferences through which you can make
modifications to
// the data in the preferences and atomically commit those changes back to the
SharedPreferences object
val editor: SharedPreferences.Editor = appSettingsPrefs.edit()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isFirstStart ){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
else{
when {
isNightModeOn -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
}
btnSwitch.setOnClickListener {
if (isNightModeOn) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
editor.putBoolean(FIRST_START, false)
editor.putBoolean(NIGHT_MODE, false)
editor.apply()
recreate() //recreate activity to make changes visible, onPause, onStop,
onDestroy, endAllActiveAnimators, onStart, onResume
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
editor.putBoolean(FIRST_START, false)
editor.putBoolean(NIGHT_MODE, true)
editor.apply()
recreate() //recreate activity to make changes visible, onPause, onStop,
onDestroy, endAllActiveAnimators, onStart, onResume
}
}
please don't mind my english ;P
您需要在 super.onCreate(savedInstanceState)
之前调用 AppCompatDelegate.setDefaultNightMode(mode)
。
先看下图
[白色模式 1]https://i.stack.imgur.com/QKcju.jpg
[白色模式 2]https://i.stack.imgur.com/QKcju.jpg
[深色模式 1]https://i.stack.imgur.com/QKcju.jpg
[深色模式 2]https://i.stack.imgur.com/QKcju.jpg
问题是 -> 当我通过单击抽屉布局设置按钮打开黑暗 activity 并在按下黑暗按钮后黑暗模式应用于整个应用程序,当我从最近的背景中删除应用程序然后打开应用程序,然后白色模式自动应用于 activity 主页,但在从抽屉布局再次打开深色 activity 后,它自动应用深色模式。
这是我实现的代码
const val FIRST_START = "FirstStart"
const val NIGHT_MODE = "NightMode"
const val PREF = "AppSettingsPrefs"
class language_settings : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loadLocate() // call LoadLocate
setContentView(R.layout.activity_language_settings)
//save preferences specific to an app, like save the dark mode again when we again
open the app
val appSettingsPrefs: SharedPreferences = getSharedPreferences(PREF, 0)
val isNightModeOn: Boolean = appSettingsPrefs.getBoolean(NIGHT_MODE, false)
val isFirstStart: Boolean = appSettingsPrefs.getBoolean(FIRST_START,false)
//Create a new Editor for these preferences through which you can make
modifications to
// the data in the preferences and atomically commit those changes back to the
SharedPreferences object
val editor: SharedPreferences.Editor = appSettingsPrefs.edit()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isFirstStart ){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
else{
when {
isNightModeOn -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
}
btnSwitch.setOnClickListener {
if (isNightModeOn) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
editor.putBoolean(FIRST_START, false)
editor.putBoolean(NIGHT_MODE, false)
editor.apply()
recreate() //recreate activity to make changes visible, onPause, onStop,
onDestroy, endAllActiveAnimators, onStart, onResume
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
editor.putBoolean(FIRST_START, false)
editor.putBoolean(NIGHT_MODE, true)
editor.apply()
recreate() //recreate activity to make changes visible, onPause, onStop,
onDestroy, endAllActiveAnimators, onStart, onResume
}
}
please don't mind my english ;P
您需要在 super.onCreate(savedInstanceState)
之前调用 AppCompatDelegate.setDefaultNightMode(mode)
。