0

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")
            }
        }
    }

UI layout

4
  • You're running the loop on the UI thread, so it blocks all UI operations, like layout and touch response. Why do you need the loop? Most everything in Android is set up to be event-driven, so it's very rarely necessary to have an infinite loop anywhere. Commented Dec 5, 2019 at 18:25
  • 1
    I need to indefinitely loop over some choices that are spoken out loud with the android TTS for my audio app. How do I write the while loop on a non UI thread? Commented Dec 5, 2019 at 18:38
  • I'm not quite sure what you mean by "loop over some choices that are spoken out loud", but it sounds like you might be better off using an UtteranceProgressListener. Commented Dec 5, 2019 at 18:44
  • I have quite a bit of logic that I have to go through in the while loop to get to the phrase that I want to speak is what I mean Commented Dec 5, 2019 at 19:33

1 Answer 1

1

Putting the testWhileLoop() function in a coroutine seems to work

    fun setupTheTestClass()
    {
        myTestClass = TestClass(cancelBtn)
        myTestClass.setupOnClickForButtons()

//        myTestClass.testWhileLoop()

        GlobalScope.launch{
            myTestClass.testWhileLoop()

        }

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.