https://kotlinlang.org logo
d

Doru N.

11/02/2020, 11:07 PM
Is there any possibility to convert the new start activity for result api into a Kotlin Completable Deferred, taking into account destroyed state of the caller Activity?
i

Ian Lake

11/02/2020, 11:09 PM
Any type of `Deferred`/inline callback won't work over configuration change or process death or recreation which is specifically why the callback needs to be registered ahead of time as per the docs: https://developer.android.com/training/basics/intents/result#register
You can certainly
tryEmit()
from that callback to your own
MutableSharedFlow
that you collect on
d

Doru N.

11/02/2020, 11:14 PM
It.s a bit new to me the usage of SharedFlow, basically I need to streamline somehow the start for result api
i

Ian Lake

11/02/2020, 11:16 PM
You could do the same thing with a
ConflatedBroadcastChannel
-
MutableSharedFlow
is the replacement as per https://blog.jetbrains.com/kotlin/2020/10/kotlinx-coroutines-1-4-0-introducing-stateflow-and-sharedflow/
👍 1
d

Doru N.

11/02/2020, 11:16 PM
My use case is that I call another process via aidl, which may return a pending intent if user has to confirm security pin, and when that completes, I return the data from activity result and continue the flow with it
i

Ian Lake

11/02/2020, 11:18 PM
the point is that the receiving the result is separate from the
launch
as they can and do occur completely separately
And there's no guarantee that any coroutine context / lambdas / etc you have available to you at
launch
time will exist when the callback fires
d

Doru N.

11/02/2020, 11:21 PM
So basically I need to break the flow of the use case into "before launch" and "after result"
My backend developer had created a function which takes a (synchronous) callback as a param of which implemention I had to provide. This implemention should consist of calling another process via aidl, which may in turn start an activity, wait for its result, and return the data to continue the function call
i

Ian Lake

11/02/2020, 11:26 PM
Yeah - your activity could be recreated due to a config change or your process killed due to low memory any time after the
launch
d

Doru N.

11/02/2020, 11:27 PM
I thought I could use suspended functions here, and make use of CompletableDeffered await which could have done the entire process very smooth
But I guess I need to reconsider creating the callback asynchronously
i

Ian Lake

11/02/2020, 11:30 PM
With the Activity Result API, you need to register your callback prior to being started - i.e., as part of your component's start up. You can't call it inline with your
launch
precisely because of the config changes and process death cases
That's intentional and what makes the Activity Result API actually something that works 100% of the time 🙂
d

Doru N.

11/02/2020, 11:33 PM
Well, thanks for all the info you provided, Ian :)