导航组件问题:使用导航组件打开对话框时出错
Navigation Component issue: errors when opening a dialog using the navigation component
当您使用导航组件打开对话框时,如果您连续快速单击该按钮两次,它会抛出一个
java.lang.IllegalArgumentException: Navigation action/destination ...cannot be found from the current destination Destination
这是合乎逻辑的,因为它已经从对话框中打开,也就是说,它试图打开自己。
用扩展解决了,有需要的可以用
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)
}
}
示例:
binding.your_view.setOnClickListenerWithDelay {
findNavController().navigate(R.id.your_action)
}
但是我想说一下这样的问题,也许有更干净的解决方案或者Google本身的解决方案
。
.
.
以及像往常一样打开对话框或导航组件哪个更好用
P.S.你可以遵循SOLID原则,将接口拆分,在SimpleCountDownTimer上缩短CountDownTimer,代码会像
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)
}
}
但我觉得这里不需要他XD
来自 IssueTracker 的回答
https://issuetracker.google.com/issues/177338151
不是导航组件错误
当您使用导航组件打开对话框时,如果您连续快速单击该按钮两次,它会抛出一个
java.lang.IllegalArgumentException: Navigation action/destination ...cannot be found from the current destination Destination
这是合乎逻辑的,因为它已经从对话框中打开,也就是说,它试图打开自己。
用扩展解决了,有需要的可以用
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)
}
}
示例:
binding.your_view.setOnClickListenerWithDelay {
findNavController().navigate(R.id.your_action)
}
但是我想说一下这样的问题,也许有更干净的解决方案或者Google本身的解决方案
。 . .
以及像往常一样打开对话框或导航组件哪个更好用
P.S.你可以遵循SOLID原则,将接口拆分,在SimpleCountDownTimer上缩短CountDownTimer,代码会像
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)
}
}
但我觉得这里不需要他XD
来自 IssueTracker 的回答
https://issuetracker.google.com/issues/177338151
不是导航组件错误