Полное решение с использованием kotlin
Шаг 1. Создайте свой класс, как следует
class MyDialog : DialogFragment() {
private var title: String? = null
private var message: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
title = it.getString(ARG_TITLE)
message = it.getString(ARG_MESSAGE)
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val myBuilder = AlertDialog.Builder(it)
myBuilder
.setTitle(title)
.setMessage(message)
.setPositiveButton("OK") { _, _ -> }
myBuilder.create()
}
}
companion object {
const val TAG = "myDialog"
private const val ARG_TITLE = "argTitle"
private const val ARG_MESSAGE = "argMessage"
fun newInstance(title: String, message: String) = MyDialog().apply {
arguments = Bundle().apply {
putString(ARG_TITLE, title)
putString(ARG_MESSAGE, message)
}
}
}
}
Шаг 2. Создайте экземпляр и покажитеэто
MyDialog.newInstance("title", "message").show(this@MyActivity.supportFragmentManager, MyDialog.TAG)
Это все!