reactormonk
09/11/2023, 1:15 PMsuspend fun loudSignIn(): GoogleSignInAccount {
val client = GoogleSignIn.getClient(this, gso)
val contract = ActivityResultContracts.StartActivityForResult()
return suspendCoroutine { cont ->
registerForActivityResult(
contract,
contract.createIntent(this, client.signInIntent)
) { activityResult ->
cont.resume(
GoogleSignIn.getSignedInAccountFromIntent(activityResult.data).asDeferred()
)
}.launch()
}.await()
}
But the Android API is WTF here that I'm getting a
is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.
What's the correct magic incantation here?Kilian
09/11/2023, 1:54 PMregisterForActivityResult
must be done before activity is resumed inside on createIan Lake
09/11/2023, 1:57 PMWhen starting an activity for a result, it is possible—and, in cases of memory-intensive operations such as camera usage, almost certain—that your process and your activity will be destroyed due to low memory.
For this reason, the Activity Result APIs decouple the result callback from the place in your code where you launch the other activity. Because the result callback needs to be available when your process and activity are recreated, the callback must be unconditionally registered every time your activity is created, even if the logic of launching the other activity only happens based on user input or other business logic.
Your activity can and will be destroyed between when you register (which, like it says, always need to be done unconditionally as part of the creation of whatever object is getting your activity result) and when the result comes in. There's no way a suspend method will ever be able to survive across process death and recreation, so this won't ever be a viable, consistent way to get an activity result
Ian Lake
09/11/2023, 1:58 PMreactormonk
09/11/2023, 2:01 PMKilian
09/11/2023, 2:12 PMcalidion
09/27/2023, 6:42 PMcalidion
09/27/2023, 6:53 PMcalidion
09/27/2023, 6:54 PMcalidion
09/27/2023, 6:57 PMIan Lake
09/27/2023, 6:59 PMcalidion
09/27/2023, 7:01 PMnew ActivityResultContracts.StartActivityForResult(),
Ian Lake
09/27/2023, 7:02 PMcalidion
09/27/2023, 7:02 PMIan Lake
09/27/2023, 7:03 PMcalidion
09/27/2023, 7:03 PMcalidion
09/27/2023, 7:04 PMcalidion
09/27/2023, 7:04 PMcalidion
09/27/2023, 7:05 PMIan Lake
09/27/2023, 7:05 PMIan Lake
09/27/2023, 7:05 PMcalidion
09/27/2023, 7:06 PMIan Lake
09/27/2023, 7:06 PMcalidion
09/27/2023, 7:07 PMcalidion
09/27/2023, 7:09 PMcalidion
09/27/2023, 7:10 PMIan Lake
09/27/2023, 7:13 PMcalidion
09/27/2023, 7:15 PMcalidion
09/27/2023, 7:16 PMcalidion
09/27/2023, 7:17 PMcalidion
09/27/2023, 7:19 PMcalidion
09/27/2023, 7:20 PM