title: How to retain selection in multi choice dialog in Android on rotation?
body: <p>I have a dialog to select more than one days of a week as follows:</p>
  
  <pre><code>class DialogSettingsEnabledDays : DialogFragment() {
      override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  
          return activity.let {
  
              val selectedDaysValue = BooleanArray(7) { _ -&gt; false }
              val selectedDaysIndex = ArrayList&lt;Int&gt;()
  
              val daysToIndexMap = mutableMapOf&lt;String, Int&gt;()
              val indexToDaysMap = mutableMapOf&lt;Int, String&gt;()
              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 -&gt;
                      if (isChecked)
                          selectedDaysIndex.add(which)
                      else if (selectedDaysIndex.contains(which))
                          selectedDaysIndex.remove(Integer.valueOf(which))
                  }
                  .setPositiveButton(R.string.dialog_ok) { _, _ -&gt;
                      if (selectedDaysIndex.isEmpty()) {
                          Toast.makeText(it, "Select atleast one day !!", Toast.LENGTH_SHORT).show()
                      } else {
                          selectedDaysIndex.sort()
                          val selectedDaysList = mutableListOf&lt;String&gt;()
                          for (i in selectedDaysIndex) {
                              selectedDaysList.add(indexToDaysMap.getValue(i))
                          }
                          val editor = prefs.edit()
                          editor
                              .putString("enabled_days", selectedDaysList.joinToString())
                              .apply()
                          val enabledDays = it.findViewById&lt;LinearLayout&gt;(R.id.settings_enabled_days)
                          enabledDays.findViewById&lt;TextView&gt;(R.id.secondary_text).text = selectedDaysList.joinToString()
                      }
                  }
                  .setNegativeButton(R.string.dialog_cancel) { _, _ -&gt; /* do nothing */ }
  
              enabledDaysBuilder.create()
          }
      }
  }
  </code></pre>
  
  <p>And I am calling this dialog in this way from my activity:</p>
  
  <pre><code>findViewById&lt;LinearLayout&gt;(R.id.settings_enabled_days)
      .setOnClickListener {
          DialogSettingsEnabledDays().show(this.supportFragmentManager, null)
  }
  </code></pre>
  
  <p>My problem is that my selection of days resets to default on rotation.
  By default I mean the selection stored in <code>SharedPreferences</code>, that is <code>selectedDaysValue</code> in <code>.setMultiChoiceItems</code>.
  Suppose, these are the selected days when the dialog pops up:</p>
  
  <pre><code>Mon, Tue, Wed, Thu, Fri
  </code></pre>
  
  <p>Now, I change the selection as:</p>
  
  <pre><code>Mon, Tue
  </code></pre>
  
  <p>But, when I rotate the phone, the selection sets back to default:</p>
  
  <pre><code>Mon, Tue, Wed, Thu, Fri
  </code></pre>
  
  <p>How can I retain my selection on orientation change?
  Because in some apps I have seen, the Dialog selection remains same on rotation.</p>