Fredrik Larsen
06/28/2019, 2:34 PMCasey Brooks
06/28/2019, 2:41 PMonCancel
callback is fired, it’s being dispatched from the Android Looper, not from your MainActivity
. The exception bubbles up to the Looper which sent the click event and triggered your callback, not the onCreate()
which declared the callback.
Even though the exception looks like it’s in the scope of that try-catch, it’s not because it’s inside a callback that’s passed to some other scope and invoked asynchronously. Generally-speaking, all async callbacks will have strange effects like this, and that’s where coroutines and RxJava starts to play a role, by making this kind of async data flow more natural to how we think of variable scoping.Fredrik Larsen
06/28/2019, 2:47 PMCasey Brooks
06/28/2019, 2:51 PMonError
callback when you subscribe
to the stream, wherever that isFredrik Larsen
06/28/2019, 2:51 PMFredrik Larsen
06/28/2019, 2:51 PMFredrik Larsen
06/28/2019, 2:52 PMCasey Brooks
06/28/2019, 2:58 PMObservable.create { ... }
), catch the exception from the callback, and pass that into the stream’s Emitter Observable.create { emitter ->
AlertDialog.Builder(requireActivity())
.setTitle("Downloading...")
.setView(view)
.setNegativeButton("Cancel") {
try {
onCancel()
}
catch(e: CancellationException) {
emitter.onError(e)
}
}
}
.create()
}
dead.fish
06/29/2019, 11:06 PMActivity
or Fragment
) implement an interface that the DialogFragment
attaches to in onActivityCreated
. This also means that you have the stream on the outside of the dialog and under your control. If you want to keep the stream inside of the AlertDialog
, you must put it into a ViewModel
(which naturally survives restarts) or any other retaining (headless) fragment.Fredrik Larsen
07/01/2019, 8:00 AM