Skip to content

Commit

Permalink
Android: make sure dialog onPostExecute methods don't run within onSt…
Browse files Browse the repository at this point in the history
…art (closes #2154)
  • Loading branch information
mhsmith committed Feb 14, 2021
1 parent 85879b6 commit 9ba7895
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ class App : Application() {
}


fun runOnUiThread(r: () -> Unit) { runOnUiThread(Runnable { r() }) }
fun runOnUiThread(r: () -> Unit) { runOnUiThread(Runnable { r() }, false) }
fun postToUiThread(r: () -> Unit) { runOnUiThread(Runnable { r() }, true) }

fun runOnUiThread(r: Runnable) {
if (onUiThread()) {
fun runOnUiThread(r: Runnable, post: Boolean) {
if (onUiThread() && !post) {
r.run()
} else {
mainHandler.post(r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,12 @@ abstract class TaskDialog<Result> : DialogFragment() {
private fun onFinished(body: () -> Unit) {
if (model.state == Thread.State.RUNNABLE) {
model.state = Thread.State.TERMINATED
body()
dismiss()

// If we're inside onStart, fragment transactions are unsafe (#2154).
postToUiThread {
body()
dismiss()
}
}
}

Expand Down

0 comments on commit 9ba7895

Please sign in to comment.