I found many similar question but none worked for me.
fun check(isLeft:Boolean){
val toast:Toast=Toast.makeText(this,"",Toast.LENGTH_SHORT)
if(isLeft){
if(left.text.toString().toInt()>right.text.toString().toInt()){
main.setBackgroundColor(Color.GREEN)
toast.cancel()
toast.setText("Correct")
toast.show()
}else{
main.setBackgroundColor(Color.RED)
toast.cancel()
toast.setText("In-Correct")
toast.show()
}
}else{
if(right.text.toString().toInt()>left.text.toString().toInt()){
main.setBackgroundColor(Color.GREEN)
toast.cancel()
toast.setText("Correct")
toast.show()
}else{
main.setBackgroundColor(Color.RED)
toast.cancel()
toast.setText("In-Correct")
toast.show()
}
}
randomNumber(left,right)
}
Toast is waiting in queue rather then being displayed intently.Even when toast.cancel() is used by me.
Is toast.cancel() depreciated?
CodePudding user response:
toast.cancel() is not deprecated and
will cancel the toast-object that you created with makeText(). If you want to cancel a previously queued toast (by calling .show()
) you must call .cancel()
on that other instance. i.e.:
val toast1:Toast = Toast.makeText(applicationContext,"One",Toast.LENGTH_LONG).show()
toast1.show()
toast1.cancel()
val toast2:Toast = Toast.makeText(applicationContext,"Two",Toast.LENGTH_SHORT).show()
toast2.show()
CodePudding user response:
As the answer has said, the cancel()
is for an instance created by makeText()
. I can understand why do you want to call cancel()
first. I think you want something like this:
toast?.apply { toast.cancel() }
toast=Toast.makeText(this,"Correct",Toast.LENGTH_SHORT)
toast.show()
And of course, you need to modify the other setText()
s as well.