I started working on a new project that is 100% written with Jetpack compose, meaning we don’t have any fragments and we’re also following the Single activity pattern.
Now I have to implement the Facebook login but I’m stuck since they’re still using the deprecated onActivityResult instead of the new contract api.
Here’s the documentation that I’m trying to follow, any help would be greatly appreciated.
Thank you all,
3
Answers
You have to wait this issue to be resolved.
For now you can pass
callbackManager
down from your activity to the Compose tree usingCompositionLocalProvider
, like this:More general solution is moving facebook logic into a view mode, and passing, then you have to create your own callback manager, something like this:
ActivityResultCallbackManager.kt
MainActivity.kt
FacebookLoginViewModel.kt
LoginScreen.kt
Also you can try building this fork, it contains changes from this pull request. It adds support of contract api, and is not yet accepted. Check out changes carefully, it’s not official!
Try this its will work:
Another approach that worked for me is to encapsulate the FB login logic in a blank activity with no UI and start facebook login as soon as it starts. As soon as you get back the results in your activity pass them back to compose function back in step 2.
Step 1: Create FB Login Activity
Step 2: Now just call this with
facebookSignRequest.launch(FBLoginActivity.getInstance(context))
from within your compose function like below.Step 3: don’t forget to add activity to manifest 😀