https://kotlinlang.org logo
Title
j

Justin Xu

04/28/2023, 8:15 AM
I am using Voyager for navigation in Compose Multiplatform. I have a google login button that should use
ActivityResultLauncher
to get the google account token and pass it to a callback in my ViewModel to authenticate. From my understanding however,
ActivityResultLaunchers
(and their callbacks) must be declared in Activity's
onCreate
before Compose's
setContent
. At the same time though, any ViewModels managed by Voyager must be created in a
Screen
(as outlined in their docs) , which is called inside
setContent
similar to a Composable. So my ViewModel callback must be present when the launcher is created before
setContent
, but is also only available inside
setContent
using Voyager. Is there a clean way around this?
I've also thought about just creating the ViewModel in the Activity, but that also seems wrong
s

SrSouza

04/28/2023, 1:16 PM
Are you using
rememberLauncherForActivityResult
?
i

Ian Lake

04/28/2023, 1:59 PM
Activity Result APIs work directly in composables: https://developer.android.com/jetpack/compose/libraries#activity_result
j

Justin Xu

04/28/2023, 6:43 PM
Ah that's right 🤦 forgot that exists thanks
I forgot to mention, I am specifically doing Google login in Compose Multiplatform. For Android specifically, there seems to be three components: 1.
GoogleSignInClient
- Must be created in an Activity, provides sign in intent to 2.
ManagedActivityResultLauncher
- Launcher for Google sign in activity 3. Sign in ViewModel function - My custom authentication in using google account token, launcher should provide Google account token to this function in launcher callback 2 can be available in Activity and Composables using
rememberLauncherForActivityResult
, but 1 is only available in Android activity, and 3 is only available in shared composables since that's where the Voyager
ScreenModels
are created. The only solutions I can think of is a. provide 1 to 3, but that would mean passing an Android dependency into my common Composables b. provide 3 to 1 by creating my ViewModel in the Activity, but that would go against Voyager's VM management inside Composables Not sure if there is a clean way around this