调用DialogFragment:Dialog的活动并不总是被解雇

时间:2018-04-30 09:01:14

标签: android kotlin dialogfragment fragmentmanager

我有一个调用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)时,对话框将再次按预期消失。

但是,在删除成功后,当我再次调用Dialog时,单击“取消”不会立即关闭对话框;相反,对话框会再次弹出,只有在第二次单击“删除”后才会消失。 FragmentManager似乎存在问题。我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

你应该致电

 getDialog().dismiss()

注意

您应该在DeleteDetailDialogFragment中创建自定义对话框。

class DeleteDetailDialogFragment : DialogFragment() {

     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View?
    {
         val rootView = inflater.inflate(R.layout.your_layout, container,false)
         return rootView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return dialog
    }

答案 1 :(得分:0)

上面的代码没有错!删除Firebase数据库中的项目会在onDataChange中触发ValueEventListener事件,然后调用updateUI函数。不幸的是,我在那里放置了onItemTouchListener处理onItemClick事件(就像通过DialogFragment我的remove()指令),只要我没有更改Firebase数据库中的任何数据,就可以正常工作。但删除一个项目会触发一个循环,导致"不稳定"我的代码的行为。解决方案是将调用从我的onItemTouchListener(处理onItemClicks)从updateUI函数移动到我的代码的onCreate部分。嗯,我学到了什么!