https://kotlinlang.org logo
#android
Title
# android
s

Shalu TD

08/04/2021, 12:51 PM
Hi gusys. I have a question related to coroutine. Coroutine block can update the UI after activity finished? If tries to update, crash happens?
c

CLOVIS

08/04/2021, 12:57 PM
It depends which thread the coroutine is running on. Only the main thread is allowed to edit the UI.
d

dammy_abayomi

08/04/2021, 1:03 PM
More so, is there something specific you want to achieve? Why do you want to update the UI since the activity has finished?
m

mkrussel

08/04/2021, 1:08 PM
The answers to all the questions are yes depending on what you do. But the practical solution is to launch the coroutine using
lifecycleScope
and then the answer to the first question becomes no, and the others don't matter.
s

Shalu TD

08/04/2021, 1:23 PM
I have tried this and found no issue while updating the UI. Actually activity is finished and trying to update UI should create leaking rt? Consider the case of our AsyncTask, if it tries to update UI after the activity is destroyed. It will give leak. I am trying to understand why in the case of Coroutine it doesn’t crash. If it will be like this, they why should we need to cancel the couroutine in onDestroy() lifecycle callback? Can we leave as it is
@mkrussel I know the case of
lifecycleScope
. The system itself cancel the coroutine if using the
lifecycleScope
.I am asking do we need to cancel the coroutine if its not crashing while trying to update UI.
r

ritesh

08/04/2021, 1:30 PM
How are you creating your co-routine? Can you paste the sample code.
s

Shalu TD

08/04/2021, 1:37 PM
@ritesh Please find the code below
Copy code
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    CoroutineScope(IO).launch {
        val data = async { downloadData() }.await()

        withContext(Main) {
            findViewById<TextView>(R.id.textview).text = data.toString()
        }
    }
}

private suspend fun downloadData(): Int {
    delay(10000)
    return 40000
}
m

mkrussel

08/04/2021, 1:39 PM
You should cancel that to prevent leaking the activity. Not sure what
findViewById
does after destruction. But it would be better to use lifecycleScope.
Copy code
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    lifecycleScope.launch {
        val data = withContext(<http://Dispatcher.IO|Dispatcher.IO>) { downloadData() }
       findViewById<TextView>(R.id.textview).text = data.toString()
    }
}
private suspend fun downloadData(): Int {
    delay(10000)
    return 40000
}
👌 1
Actually if you follow Google's and JetBrains advice, all suspend functions should be main safe, so the
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
should be in
downloadData
.
s

Shalu TD

08/04/2021, 1:44 PM
@mkrussel I know all things you are saying. I am asking only question that why its not crashing after the activity is destroyed
You are saying, we need to cancel to prevent leak. why that leak is not happening here. Here I am not canceling 🙂
m

mkrussel

08/04/2021, 1:45 PM
It doesn't crash because you are leaking the Activity. So the object still exists and you can modify it, you just should not do that.
I could also imagine that the behavior of
findViewById
could change with different API levels, so it might work now but not work in the future.
s

Shalu TD

08/04/2021, 1:55 PM
Thanks @mkrussel