Search code examples
androidkotlindialogfragment

Dismissing dialog and showing new one shortly shows underlying fragment before opening new dialog


I currently have one fragment that shows dialogfragment, and when I press the button on dialogfragment, I want to show another dialogfragment. In my first dialog fragment I have following method:

 fun showSecondDialog() {
    dismiss()
    SecondDialog().showDialog(targetFragment!!)
}

showDialog() looks like this:

    fun showDialog(fragment: Fragment) {
    val fragmentManager = fragment.fragmentManager
    val ft = fragmentManager!!.beginTransaction()
    val prev = fragmentManager.findFragmentByTag(getName())
    if (prev != null) {
        ft.remove(prev)
    }
    ft.addToBackStack(null)

    setTargetFragment(fragment, 0)
    show(ft, getName())
}

The problem is, that dismissing dialog works immediately, but before the new dialog is shown there is a gap, where my underlying fragment is fully visible and this causes an undesirable flicker.

How to show second dialog immediately, or how to know when it's opened, so I could close the first one?


Solution

  • I am also having this same problem, but you can try a workaround like this if it works according to your requirement :

    fun showSecondDialog() {
    
        SecondDialog().showDialog(targetFragment!!)
    
        Handler().postDelayed({ dismiss() }, 1000)
    
    }