Launching coroutines inside Alert Dialog Builder

Issue

If I have a suspend function running inside a coroutine and inside that function I build an alertDialog, does this mean that everything inside the builder is running separately from my coroutine? If I launch a coroutine from the same scope there, is it considered nested?

Example code:

    coroutineScope.launch{
        foo()
    }
    suspend fun foo(){
        ....
        AlertDialog.Builder(activity)
            .setOnDismissListener {
                coroutineScope.launch { foo() }
            }.show()
    }

Does the first scope has the second one as nested or the first coroutine finished its job?

Solution

The first coroutine scope finishes once the dialog is created and all its children are cancelled, that’s why you cannot initiate a suspend function from inside the alert dialog builder. So the second coroutine you’re launching is not nested.

Answered By – Littlish

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published