Android dialog報錯 The specified child already has a parent. You must call removeView on the child's parent first
問題描述
正在學kotlin的時候,寫了一個簡單的彈窗管理框架,但是最近發現彈窗消失後重新show,會出這個bug
解決方案
open class BaseDialog(context: Context) {
val mContext = context
val dialog = AlertDialog.Builder(context)
var alert: AlertDialog? = null
fun show() {
alert = dialog.show()
}
fun dismiss() {
alert?.dismiss()
}
}
修改後的代碼:
open class BaseDialog(context: Context) {
val mContext = context
val dialog = AlertDialog.Builder(context)
var alert: AlertDialog? = null
fun show() {
alert?.show()
}
fun dismiss() {
alert?.dismiss()
}
}
提前在 dialog.create() 的時候就賦值給 alert 就可以了