title: What is a good way of creating dialog in android in kotlin?
body: <p>I have a SettingsActivity.kt as follows:</p>
  
  <pre><code>class SettingsActivity : AppCompatActivity() {
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
  
          setLayout()
          setListeners()
  
      }
  
      private fun setLayout() {/* fun to set layout* /}
      private fun setListeners() {
          val day = findViewById&lt;LinearLayout&gt;(R.id.settings_day)
          day.setOnClickListener { myDialog() }
  
          /* some other dialogs created in similar way */
      }
  
      private fun myDialog() {
          val prefs = getSharedPreferences("SETTINGS", Context.MODE_PRIVATE)
          var selectedDay = prefs.getInt("day", 1)
  
          val myBuilder = AlertDialog.Builder(this)
          myBuilder
              .setTitle(R.string.settings_day)
              .setSingleChoiceItems(R.array.days, selectedDay) { _, which -&gt;
                  selectedDay = which
              }
              .setPositiveButton(R.string.dialog_ok) { _, _ -&gt;
                  val editor = prefs.edit()
                  editor
                      .putInt("day", selectedDay)
                      .apply()
              }
              .setNegativeButton(R.string.dialog_cancel) { _, _ -&gt; /* do nothing */ } 
  
          val theDialog = myBuilder.create()
          theDialog.show()
      }
  }
  </code></pre>
  
  <p>When the orientation of device changes, the dialog disappears.</p>
  
  <p>I think I have to use DialogFragment, but I have some problems,
  the official guide at <a href="https://developer.android.com/guide/topics/ui/dialogs#kotlin" rel="nofollow noreferrer">https://developer.android.com/guide/topics/ui/dialogs#kotlin</a> doesn't explain much. I am confused where to place the code.</p>
  
  <p>Most of the tutorials on DialogFragment are either for custom layout or in java.</p>
  
  <p>So, can somebody tell me how to convert my code
  so as to use DialogFragment.
  I am having difficulty to understand it from examples.</p>