A while loop is blocking the UI of my app from showing so I have no chance of pushing the cancel button to break out of it. Also if I am able to get the UI to show by calling the setup function from a TextToSpeech OnInitListener block, I still cant register the button press and exit out.
Why is this happening and how can I get normal, non blocking while loop behavior that I can exit out of with a button click from my cancel button in the UI?
This onCreate does not show the UI at all
class MainActivity : AppCompatActivity() {
lateinit var myTestClass: TestClass
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupTheTestClass()
}
This onCreate shows the UI as I call the setupTheTestClass() within the TextToSpeech OnInitListener but won't register the button press to let me exit the while loop
class MainActivity : AppCompatActivity() {
lateinit var myTestClass: TestClass
lateinit var mTTS:TextToSpeech
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mTTS = TextToSpeech(applicationContext, TextToSpeech.OnInitListener { status ->
setupTheTestClass()
})
}
fun setupTheTestClass()
{
myTestClass = TestClass(cancelBtn)
myTestClass.setupOnClickForButtons()
myTestClass.testWhileLoop() // blocks UI in first example
}
class TestClass(val cancelBtn: Button) {
var running: Boolean = true
fun setupOnClickForButtons()
{
cancelBtn.setOnClickListener {
running = false
println("no longer running")
}
}
fun testWhileLoop() {
while (running == true) {
println("running")
}
}
}

UtteranceProgressListener.