【发布时间】:2018-04-30 09:01:14
【问题描述】:
我有一个像这样调用 DialogFragment 的 Activity:
private fun showDeleteDetailDialog(itemView: View, categoryId: String, detailId: String) {
val dialog = DeleteDetailDialogFragment.newInstance(categoryId, detailId)
dialog.show(this@DetailsActivity.fragmentManager, "DeleteDetailDialog")
}
这是我的 DialogFragment 的代码(单击 PositiveButton 会删除 Firebase 数据库中的一个项目):
class DeleteDetailDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Use the Builder class for convenient dialog construction
val categoryId = arguments.getString(ARG_CATEGORY_ID)
val detailId = arguments.getString(ARG_DETAIL_ID)
val builder = AlertDialog.Builder(activity)
builder.setMessage(R.string.delete_detail)
.setPositiveButton(R.string.delete, { dialog, id ->
deleteDetail(categoryId, detailId)
})
.setNegativeButton(R.string.cancel, { dialog, id ->
// User cancelled the dialog
})
// Create the AlertDialog object and return it
return builder.create()
}
private fun deleteDetail(categoryId: String, detailId: String) {
// get the detail reference for the specified category
val deleteRef = FirebaseDatabase.getInstance().getReference("details").child(categoryId).child(detailId)
// remove detail
deleteRef.removeValue()
// get the reference for the specified favorite, identified by detailId
val deleteFaveRef = FirebaseDatabase.getInstance().getReference("favorites").child(detailId)
// remove favorite
deleteFaveRef.removeValue()
}
companion object {
private val ARG_CATEGORY_ID = "category_id"
private val ARG_DETAIL_ID = "detail_id"
fun newInstance(categoryId: String, detailId: String): DeleteDetailDialogFragment {
val fragment = DeleteDetailDialogFragment()
val args = Bundle()
args.putString(ARG_CATEGORY_ID, categoryId)
args.putString(ARG_DETAIL_ID, detailId)
fragment.arguments = args
return fragment
}
}
}
当我调用 Dialog 时,会弹出 Dialog 窗口。然后当我单击取消(NegativeButton)时,对话框按预期消失。当我单击删除(PositiveButton)时,对话框再次按预期消失。
但是,在成功删除后,当我再次调用对话框时,单击取消不会立即关闭对话框;相反,对话框再次弹出,仅在第二次单击删除后消失。 FragmentManager 似乎存在问题。我在这里错过了什么?
【问题讨论】:
标签: android kotlin dialogfragment fragmentmanager