Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Facebook Sign in CallbackManager with onActivityResult deprecated?

Im creating app with Facebook sign in option. I did everything so far, but having problem with the result after from the Facebook App. I followed Google's documentation, but that one is not updated with the newly deprecated onActivityResult function. Now, I have already used the new way of getting result from another activity from this. However, this is not applicable on the Facebook sign in implementation due to not using the the registerForActivityResult launcher, but it uses its own callback manager.

Has anyone come up with a solution to this problem? Maybe I'm missing something out.

 callbackManager = CallbackManager.Factory.create()
    signInWithFacebook.setReadPermissions("email", "public_profile")

    signInWithFacebook.registerCallback(
        callbackManager,
        object : FacebookCallback<LoginResult> {
            override fun onSuccess(result: LoginResult) {
                handleFacebookAccessToken(result.accessToken)
            }

            override fun onCancel() {
     
            }

            override fun onError(error: FacebookException?) {
              
            }
        })




override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        callbackManager.onActivityResult(requestCode, resultCode, data)
    }


private fun handleFacebookAccessToken(token: AccessToken) {
    val credential = FacebookAuthProvider.getCredential(token.token)
    auth.signInWithCredential(credential)
        .addOnCompleteListener(OnCompleteListener {
            if (it.isSuccessful) {

            } else {
              
            }
        })
} 
like image 508
Andrej Avatar asked Sep 06 '25 09:09

Andrej


1 Answers

Using this(https://github.com/facebook/facebook-android-sdk/issues/875) Github issue I found your answer and here is summarize

First of all, initialize callback manager

val callbackManager: CallbackManager = CallbackManager.Factory.create()

then register callback using a login manager

 LoginManager.getInstance().registerCallback(callbackManager,
        object : FacebookCallback<LoginResult?> {
            override fun onSuccess(result: LoginResult?) {
                if (result?.accessToken != null) {
                    val accessToken = result.accessToken.token
        
                }
            }

            override fun onCancel() {
               
            }

            override fun onError(error: FacebookException) {
            }
        })

on your custom Facebook button, you have to setOnClickListner

 binding.btnFacebook.setOnClickListener {
        LoginManager.getInstance().logInWithReadPermissions(
            this,
            loginViewModel.callbackManager,
            listOf("public_profile", "email")
        )
    }

what we have to change for onActivityResult Deprecated

Previously

binding.btnFacebook.setOnClickListener {
            LoginManager.getInstance().logInWithReadPermissions(
                this,
                listOf("public_profile", "email")
            )
        }

Now

 binding.btnFacebook.setOnClickListener {
            LoginManager.getInstance().logInWithReadPermissions(
                this,
                loginViewModel.callbackManager, //we added callback here according to new sdk 12.0.0 version of facebook
                listOf("public_profile", "email")
            )
    }

Now I am using Facebook SDK version

  //facebook login
    implementation 'com.facebook.android:facebook-login:12.0.0'
like image 84
Vishal kumar singhvi Avatar answered Sep 09 '25 02:09

Vishal kumar singhvi