question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Launch inside callback of of AlertDialog not executing code

See original GitHub issue

When i place a launch statement inside the callback of an AlertDialog the code does not get executed. Calling the launch on GlobalScope fixes it.

import android.os.Bundle
import android.util.Log
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.Button
import android.widget.FrameLayout
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    companion object {

        private val TAG = MainActivity::class.java.simpleName
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(FrameLayout(this).also { it.addView(Button(this).apply {
            text = "ask me"
            setOnClickListener { onButtonClicked() }
        })}, FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT))
    }

    private fun onButtonClicked() = GlobalScope.launch(Dispatchers.Main) {
        AlertDialog.Builder(this@MainActivity).setMessage("Do it?").setPositiveButton("Yes") { _, _ ->
            Log.w(TAG, "answered yes")
            launch {
                Log.w(TAG, "inside launch without GlobalScope") // this does not get executed
            }
            GlobalScope.launch {
                Log.w(TAG, "inside launch with GlobalScope")
            }
        }.setNegativeButton("No") { _, _ ->}.show()
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
AllanWangcommented, Jan 15, 2019

This is because your inner launch is executed within the job of your parent launch. As the parent finishes once the dialog is created, all children are cancelled.

Using global scope again avoids that hierarchy, so they are independent

0reactions
mtangoocommented, May 22, 2020

for completion just use lifecycleScope as launcher and it will work fine!

        AlertDialog.Builder(this@MainActivity).setMessage("Do it?").setPositiveButton("Yes") { _, _ ->
            Log.w(TAG, "answered yes")
            lifecycleScope.launch {
                Log.w(TAG, "inside launch with lifecycleScope") 
            }
        }.show()
Read more comments on GitHub >

github_iconTop Results From Across the Web

android - AlertDialog sends no callback when created
AlertDialog sends no callback when created ... I have this simple code: AlertDialog dialog = new AlertDialog.Builder(this).create(); dialog.
Read more >
Not getting the Alert Dialog to Show. No errors in code.
Hey so I did exactly as the video instructed. I couldn't get the Alert Dialog Box to show. My app does run and...
Read more >
Callback on AlertDialog - java - Code Review Stack Exchange
I am using an AlertDialog in Android, asking the user for some input. I would like do run some code when the user...
Read more >
Dialogs - Android Developers
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill...
Read more >
Adding AlertDialog with Jetpack Compose to Android apps
Learn how to use Jetpack Compose in an Android app to create an alert ... we can pass a callback function that should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found