我有一个对话框来选择一周中的一天以上,如下所示:
class DialogSettingsEnabledDays : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val selectedDaysValue = BooleanArray(7) { _ -> false }
val selectedDaysIndex = ArrayList<Int>()
val daysToIndexMap = mutableMapOf<String, Int>()
val indexToDaysMap = mutableMapOf<Int, String>()
val daysArray = resources.getStringArray(R.array.days_medium)
for (i in 0..6) {
daysToIndexMap[daysArray[i]] = i
indexToDaysMap[i] = daysArray[i]
}
val prefs = it!!.getSharedPreferences(getString(R.string.shared_prefs_settings), Context.MODE_PRIVATE)
val selectedDaysString = prefs.getString("enabled_days", getString(R.string.default_enabled_days))
val selectedDays = selectedDaysString!!.split(", ")
for (day in selectedDays) {
selectedDaysValue[daysToIndexMap.getValue(day)] = true
selectedDaysIndex.add(daysToIndexMap.getValue(day))
}
val enabledDaysBuilder = AlertDialog.Builder(it)
enabledDaysBuilder
.setTitle(R.string.settings_enabled_days)
.setMultiChoiceItems(R.array.days_long, selectedDaysValue) { _, which, isChecked ->
if (isChecked)
selectedDaysIndex.add(which)
else if (selectedDaysIndex.contains(which))
selectedDaysIndex.remove(Integer.valueOf(which))
}
.setPositiveButton(R.string.dialog_ok) { _, _ ->
if (selectedDaysIndex.isEmpty()) {
Toast.makeText(it, "Select atleast one day !!", Toast.LENGTH_SHORT).show()
} else {
selectedDaysIndex.sort()
val selectedDaysList = mutableListOf<String>()
for (i in selectedDaysIndex) {
selectedDaysList.add(indexToDaysMap.getValue(i))
}
val editor = prefs.edit()
editor
.putString("enabled_days", selectedDaysList.joinToString())
.apply()
val enabledDays = it.findViewById<LinearLayout>(R.id.settings_enabled_days)
enabledDays.findViewById<TextView>(R.id.secondary_text).text = selectedDaysList.joinToString()
}
}
.setNegativeButton(R.string.dialog_cancel) { _, _ -> /* do nothing */ }
enabledDaysBuilder.create()
}
}
}
我在活动中以这种方式调用此对话框:
findViewById<LinearLayout>(R.id.settings_enabled_days)
.setOnClickListener {
DialogSettingsEnabledDays().show(this.supportFragmentManager, null)
}
我的问题是我选择的天数在轮换时重置为默认值。
默认情况下,我的意思是存储在SharedPreferences
中的选择,即selectedDaysValue
中的.setMultiChoiceItems
。
假设这些是弹出对话框的选定日期:
Mon, Tue, Wed, Thu, Fri
现在,我将选择更改为:
Mon, Tue
但是,当我旋转手机时,选择项会恢复为默认设置:
Mon, Tue, Wed, Thu, Fri
如何保留更改方向的选择? 因为在我看到的某些应用中,对话框选择在旋转时保持不变。
答案 0 :(得分:1)
Android系统将自动恢复您的片段状态,而状态更改后的片段实际上并没有被破坏,而是仅重新创建其视图,因此片段变量内的值将保持原样,您所要做的重新分配变量值到您的视图,这是参考链接https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
答案 1 :(得分:0)
好吧,这是我仅通过更改DialogFragment
代码而做出的最终解决方案:
更改要存储的数据范围:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val selectedDaysValue = BooleanArray(7) { _ -> false }
val selectedDaysIndex = ArrayList<Int>()
收件人:
private var selectedDaysValue = BooleanArray(7) { _ -> false }
private var selectedDaysIndex = ArrayList<Int>()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
存储数据:
override fun onSaveInstanceState(outState: Bundle) {
outState.putBooleanArray("selected_days_value", this.selectedDaysValue)
outState.putIntegerArrayList("selected_days_index", this.selectedDaysIndex)
}
我在哪里读取数据为:
val prefs = it!!.getSharedPreferences(getString(R.string.shared_prefs_settings), Context.MODE_PRIVATE)
val selectedDaysString = prefs.getString("enabled_days", getString(R.string.default_enabled_days))
val selectedDays = selectedDaysString!!.split(", ")
for (day in selectedDays) {
selectedDaysValue[daysToIndexMap.getValue(day)] = true
selectedDaysIndex.add(daysToIndexMap.getValue(day))
}
从保存状态读取为:
val prefs = activity!!.getSharedPreferences(getString(R.string.shared_prefs_settings), Context.MODE_PRIVATE)
if (savedInstanceState == null) {
val selectedDaysString = prefs.getString("enabled_days", getString(R.string.default_enabled_days))
val selectedDays = selectedDaysString!!.split(", ")
for (day in selectedDays) {
selectedDaysValue[daysToIndexMap.getValue(day)] = true
selectedDaysIndex.add(daysToIndexMap.getValue(day))
}
} else {
with(savedInstanceState) {
selectedDaysValue = getBooleanArray("selected_days_value")!!
selectedDaysIndex = getIntegerArrayList("selected_days_index")!!
}
}