When you open a dialog using the Navigation Component, if you quickly click on the button twice in a row, it throws an
java.lang.IllegalArgumentException: Navigation action/destination ...cannot be found from the current destination Destination
Which is logical because it opens already from the dialog, that is, it tries to open itself.
Solved it with Extension, if anyone needs you can use
fun View.setOnClickListenerWithDelay(listener: (View) -> Unit) {
this.setOnClickListener {
it.isEnabled = false
object : CountDownTimer(1000, 1000) {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
it.isEnabled = true
}
}.start()
listener(it)
}
}
EXAMPLE:
binding.your_view.setOnClickListenerWithDelay {
findNavController().navigate(R.id.your_action)
}
But I would like to say about such a problem, maybe there is a cleaner solution or a solution from Google itself
. . .
And what is better to use to open dialogs as usual or navigation components
P.S. You can follow the SOLID principle and split the interface and shorten CountDownTimer on SimpleCountDownTimer and the code will look like
open class SimpleCountDownTimer(
millisInFuture: Long, countDownInterval: Long
) : CountDownTimer(millisInFuture, countDownInterval) {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
}
}
fun View.setOnClickListenerWithDelay(listener: (View) -> Unit) {
this.setOnClickListener {
it.isEnabled = false
object : SimpleCountDownTimer(1000, 1000) {
override fun onFinish() {
it.isEnabled = true
}
}.start()
listener(it)
}
}
but I think he is not needed here XD
Answer from IssueTracker
https://issuetracker.google.com/issues/177338151
It's not Navigation component error