Launch inside callback of of AlertDialog not executing code
See original GitHub issueWhen 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:
- Created 5 years ago
- Reactions:2
- Comments:10 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is because your inner
launch
is executed within the job of your parentlaunch
. As the parent finishes once the dialog is created, all children are cancelled.Using global scope again avoids that hierarchy, so they are independent
for completion just use lifecycleScope as launcher and it will work fine!