【发布时间】:2020-02-04 19:12:04
【问题描述】:
当我在ListPreference 中选择一个项目来更改我的应用程序的主题时,如何让我的Activity 重新创建自己并保持特定主题?出于某种原因,无论选择 ListPreference 中的哪个项目,我的应用都停留在浅色主题上。我不确定我在这里做错了什么。
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light"/>
<style name="MyDarkSettingsTheme" parent="Theme.AppCompat"/>
<style name="MyLightSettingsTheme" parent="Theme.AppCompat.Light"/>
</resources>
活动
class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
// Declaring initial value for applying appropriate Theme
private var mCurrentValue: Boolean = true // True is the default value
override fun onCreate(savedInstanceState: Bundle?) {
// Checking which Theme should be used. IMPORTANT: applying Themes MUST called BEFORE super.onCreate() and setContentView!!!
val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
mCurrentValue = mSharedPreferences.getBoolean("light", true)
if (mCurrentValue) setTheme(R.style.MyLightSettingsTheme)
else setTheme(R.style.MyDarkSettingsTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
// In order to recreate Activity, we must check the value here. Because, when we come back from another Activity, the onCreate isn't called again.
override fun onStart() {
super.onStart()
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
val mFrameLayout = findViewById<FrameLayout>(R.id.settings)
val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val mNewValue = mSharedPreferences.getBoolean("light", true)
// If value differs from previous Theme, recreate Activity
if (mCurrentValue != mNewValue) recreate()
// if (mNewValue) {
// mFrameLayout.setBackgroundColor(Color.WHITE)
// }
// else {
// mFrameLayout.setBackgroundColor(Color.BLACK)
// }
// ... do other stuff here
}
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
override fun onDestroy() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
//...
super.onDestroy()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when (key) {
"list_theme" -> {
if (sharedPreferences.getString(key, "light") == "light") {
setTheme(R.style.MyLightSettingsTheme)
recreate()
}
else if (sharedPreferences.getString(key, "dark") == "dark") {
setTheme(R.style.MyDarkSettingsTheme)
recreate()
}
}
}
}
}
1) 活动打开
2) ListPreference 点击
3) 选择深色偏好
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> {
val intentSettings = Intent(this, SettingsActivity ::class.java)
startActivity(intentSettings)
true
}
else ->
super.onOptionsItemSelected(item)
}
}
}
【问题讨论】:
标签: android sharedpreferences android-theme android-preferences