我为我的应用实现了昼/夜主题,并在我的PreferencesActivity / Fragment中添加了ListPreference来在其中进行更改。一切都适用于API 24+,但不适用于API 23(这是我的最低要求)。
问题:在“偏好设置”中选择其他主题,然后返回MainActivity时,它将应用新主题,然后冻结。
我在日志中看到MainActivity正在重新创建自身,再次经历生命周期,但是在最后一个onResume()之后立即调用onPause()。
从MainActivity启动PreferencesActivity:
06-02 23:29:17.795 11783-11783/com.package D/MainActivity: onPause()
06-02 23:29:17.815 11783-11783/com.package D/PreferencesActivity: onResume()
06-02 23:29:18.299 11783-11783/com.package D/RosterParentFragment: saving instance state with 2019-06
切换主题:
06-02 23:29:29.853 11783-11783/com.package D/PreferencesFragment: Preference changed with key KEY_DAY_NIGHT_MODE
06-02 23:29:29.856 11783-11783/com.package D/PreferencesActivity: onPause()
06-02 23:29:29.870 11783-11783/com.package D/PreferencesActivity: onResume()
切换回MainActivity(通过按回机或主页<-):
06-02 23:32:26.209 11783-11783/com.package D/MainActivity: onResume()
06-02 23:32:26.247 11783-11783/com.package D/MainActivity: onPause()
06-02 23:32:26.248 11783-11783/com.package D/RosterParentFragment: saving instance state with 2019-06
06-02 23:32:26.254 11783-11783/com.package D/RosterParentFragment: onCreate()
06-02 23:32:26.254 11783-11783/com.package D/RosterParentFragment: savedInstanceState == Bundle[{android:support:fragments=androidx.fragment.app.FragmentManagerState@9a98da, androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], SelectedYearMonth=2019-06, android:view_state={2131296460=FragmentPager.SavedState{f01b10b position=113}}}]
06-02 23:32:26.254 11783-11783/com.package D/MainActivity: onCreate()
06-02 23:32:26.266 11783-11783/com.package D/MainActivity: savedInstanceState NOT null
06-02 23:32:26.266 11783-11783/com.package D/MainActivity: got fragment from fragment manager
06-02 23:32:26.268 11783-11783/com.package D/RosterParentFragment: onCreateView()
06-02 23:32:26.353 11783-11783/com.package D/MainActivity: onResume()
06-02 23:32:26.366 11783-11783/com.package D/MainActivity: onPause()
(这是最后一个onPause()似乎是罪魁祸首-不针对API 24+这么做。)
此后,触摸屏幕将显示一个
W/ViewRootImpl: Dropping event due to no window focus: MotionEvent
警告并远离应用程序(例如,主页按钮)会提供
Performing pause of activity that is not resumed
适用于API 24+。有什么想法吗?
class PreferencesFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
private val dayNightMode: ListPreference by lazy { findPreference<ListPreference>(Preferences.Key.DAY_NIGHT_MODE)!! }
private val dayNightModeEntries = App.instance.applicationContext.resources.getStringArray(R.array.preferences_theme_entries)
private val dayNightModeValues = App.instance.applicationContext.resources.getStringArray(R.array.preferences_theme_values)
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
Preferences.Key.DAY_NIGHT_MODE -> {
AppCompatDelegate
.setDefaultNightMode(
Preferences.getCurrentDayNightMode())
}
}
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
override fun onStart() {
super.onStart()
Preferences.default()
.registerOnSharedPreferenceChangeListener(this)
}
override fun onStop() {
Preferences.default()
.unregisterOnSharedPreferenceChangeListener(this)
super.onStop()
}
}
object Preferences {
object Key {
val DAY_NIGHT_MODE: String = "KEY_DAY_NIGHT_MODE"
}
fun default(): SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(App.instance.applicationContext)
fun getCurrentDayNightMode(): Int {
if (default().getString(Key.DAY_NIGHT_MODE, null) == null) {
// no DayNightMode selected - setting to MODE_NIGHT_FOLLOW_SYSTEM
default().setValue(Key.DAY_NIGHT_MODE, MODE_NIGHT_FOLLOW_SYSTEM.toString())
}
return default().getString(Key.DAY_NIGHT_MODE, null)!!.toInt()
}
fun setCurrentDayNightMode(mode: Int) {
AppCompatDelegate.setDefaultNightMode(mode)
default().setValue(Key.DAY_NIGHT_MODE, mode.toString())
}
}