我的目标:通过偏好设置夜间模式并实时更新 UI。
到目前为止:完成。但是,当我单击返回标题的首选项屏幕时,我无法再次返回不同的首选项屏幕。
详细说明:那时我的设置非常简单。我遵循SettingsActivity的Android Studio(3.1.4)设置的预设,具有AppCompatPreferenceActivity模板。我有一个主屏幕和两个更深的屏幕。
我的第一个屏幕有两个选择:General 和 About。选择 General 后,我加载了一个带有 Switch 首选项的 GeneralPreferenceFragment,即“夜间模式”。如果我设置它,它会实时切换主题,当我返回时,它也会在我的第一个设置屏幕上完成。
问题:当我更改主题时,返回主设置屏幕并尝试重新访问常规或关于屏幕,我不能再深入了!如果我将偏好切换为偶数,以便最终进入初始主题,我可以像什么都没发生一样访问它们。
SettingsActivity 类
class SettingsActivity : AppCompatPreferenceActivity() {
// To be used for live changes in night mode
private var mCurrentNightMode: Int = 0
private val TAG = "SettingsActivity"
private var mThemeId = 0
/**
* Doing this hack to add my own actionbar on top since it wasn't there on its own
*/
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
Log.d(TAG, "onPostCreate")
val root = findViewById<View>(android.R.id.list).parent.parent.parent as LinearLayout
val bar = LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false) as Toolbar
root.addView(bar, 0) // insert at top
bar.setNavigationOnClickListener {finish()}
}
override fun setTheme(resid: Int) {
super.setTheme(resid)
mThemeId = resid
}
override fun onCreate(savedInstanceState: Bundle?) {
if (delegate.applyDayNight() && (mThemeId != 0) ) {
// If DayNight has been applied, we need to re-apply the theme for
// the changes to take effect. On API 23+, we should bypass
// setTheme(), which will no-op if the theme ID is identical to the
// current theme ID.
if (Build.VERSION.SDK_INT >= 23) {
onApplyThemeResource(theme, mThemeId, false);
} else {
setTheme(mThemeId);
}
}
super.onCreate(savedInstanceState)
Log.d("SettingsActivity", "onCreate")
setupActionBar()
// Storing the current value so if we come back we'll check on postResume for any changes
mCurrentNightMode = getCurrentNightMode();
}
// Comparing current and last known night mode value
private fun hasNightModeChanged(): Boolean {
return mCurrentNightMode != getCurrentNightMode()
}
private fun getCurrentNightMode(): Int {
return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
}
override fun onPostResume() {
super.onPostResume()
// Self-explanatory. When I load back into this screen, if there's a change in the theme, load it back!
if(hasNightModeChanged()) {
recreate()
}
}
/**
* Set up the [android.app.ActionBar], if the API is available.
*/
private fun setupActionBar() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
/**
* {@inheritDoc}
*/
override fun onIsMultiPane(): Boolean {
return isXLargeTablet(this)
}
/**
* {@inheritDoc}
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
override fun onBuildHeaders(target: List<PreferenceActivity.Header>) {
loadHeadersFromResource(R.xml.pref_headers, target)
}
/**
* This method stops fragment injection in malicious applications.
* Make sure to deny any unknown fragments here.
*/
override fun isValidFragment(fragmentName: String): Boolean {
return PreferenceFragment::class.java.name == fragmentName
|| GeneralPreferenceFragment::class.java.name == fragmentName
|| DataSyncPreferenceFragment::class.java.name == fragmentName
|| NotificationPreferenceFragment::class.java.name == fragmentName
|| AboutFragment::class.java.name == fragmentName
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class GeneralPreferenceFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.pref_general)
setHasOptionsMenu(true)
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
findPreference("night_mode").setOnPreferenceChangeListener { preference, newValue ->
val booleanValue = newValue as Boolean
if(booleanValue) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
activity.recreate()
true
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
startActivity(Intent(activity, SettingsActivity::class.java))
return true
}
return super.onOptionsItemSelected(item)
}
}
.
.
.
companion object {
// (didn't change anything here)
}
我认为在我的“重新创建”调用中会出现问题。这就像首选项列表的 onItemClickListener 为 null 或类似的东西。
任何人都可以帮忙吗?
编辑:简化,现在我的所有逻辑都在 SettingsActivity 类中,我不需要在抽象类中拥有它