facebook-android-sdk icon indicating copy to clipboard operation
facebook-android-sdk copied to clipboard

Question: how to implement login callback using registerForActivityResult?

Open abadoliverco opened this issue 3 years ago • 14 comments

onActivityResult is already deprecated, what are the best practices or best approach to use registerForActivityResult for the login callback?

thank you!

abadoliverco avatar Apr 14 '21 15:04 abadoliverco

What a shame that this is still not answered, boo facebook

Berki2021 avatar Jun 27 '21 10:06 Berki2021

Any solution?

frankmungnodev avatar Jul 29 '21 10:07 frankmungnodev

Hi @abadoliverco, thanks for raising this issue and thanks for @Berki2021 and @frankmungnodev 's attention! We will release v12.0 in early October to address this issue. On v12.0, all Facebook buttons and dialogs will automatically detect the context and use the new APIs to handle activity results. You will not need to override onActivityResult in v12.0.

linmx0130 avatar Sep 22 '21 19:09 linmx0130

@linmx0130 Facebook v.12 is now released, how should we change our code in order to not use onActivityResult anymore? I am not using the facebook buttons or dialog and have my own implementation. Still I need to override onActivityResult...

Could someone please give an example??

Berki2021 avatar Oct 16 '21 09:10 Berki2021

Yes, we need Facebook to update the documentation and also there are examples with Jetpack Compose, I am developing a new app on Android and I can't implement the Login with Facebook because there is no documentation about this!

MauricioDomenech avatar Oct 16 '21 09:10 MauricioDomenech

Hi @Berki2021! I'm not sure how you implement the login flow. If you're using LoginManager.logIn(), LoginManager. logInWithReadPermissions() and LoginManger.logInWithPublishPermissions(), you can choose the functions that receives an ActivityResultRegistryOwner instance and a CallbackManager instance to avoid override onActivityResult.

For example, you can use the this method https://github.com/facebook/facebook-android-sdk/blob/5dfcde5751abd7ecb250229ee58d7316bd48d31d/facebook-common/src/main/java/com/facebook/login/LoginManager.java#L581-L584 to replace the original logInWithReadPermissions().

Thanks @MauricioDomenech 's suggestions on Jetpack Compose examples. We are working on building a example app with Jetpack Compose and it will be released soon. Basically, you can use AndroidView to present the facebook login button and follow the original flow.

linmx0130 avatar Oct 20 '21 17:10 linmx0130

Hi there! I am trying to update to 12+ and I'm a bit unclear on how to use the callback manager without onActivityResult. Has a sample been made? @linmx0130 I'm not using the Login button either and I already have a callback manager and a login callback created, but it was working by forwarding the call of onActivityResult to the callback manager. Now, after trying to update, I am no longer getting responses and I'm unsure if it's because I need to do something else for the update. I did pass my callback manager to logInWithReadPermissions.

georgii11 avatar Dec 23 '21 17:12 georgii11

Hi @georgii11! Thanks for the question! We will release a sample app early next year. For the logInWithReadPermissions method that receives a callback manager as the second parameter, the callback manager should call your login callback.

An sample login screen built with Jetpack Compose is like

@Composable
fun LoginMenuScreen() {
  val context = LocalContext.current
  Column(verticalArrangement = Arrangement.spacedBy(4.dp), modifier = Modifier.padding(16.dp)) {
    MenuItem(
        "Login",
        onClick = {
          if (context is ActivityResultRegistryOwner) {
            val callbackManager = CallbackManager.Factory.create()
            val loginManager = LoginManager.getInstance()
            loginManager.registerCallback(
                callbackManager,
                object : FacebookCallback<LoginResult> {
                  override fun onCancel() {
                    Toast.makeText(context, "Login canceled!", Toast.LENGTH_LONG).show()
                  }

                  override fun onError(error: FacebookException) {
                    Log.e("Login", error.message ?: "Unknown error")
                    Toast.makeText(context, "Login failed with errors!", Toast.LENGTH_LONG).show()
                  }

                  override fun onSuccess(result: LoginResult) {
                    Toast.makeText(context, "Login succeed!", Toast.LENGTH_LONG).show()
                  }
                })
            LoginManager.getInstance().logIn(context, callbackManager, listOf("email"))
          } else {
            Toast.makeText(
                    context,
                    "This login should only happens with an AndroidX activity.",
                    Toast.LENGTH_LONG)
                .show()
          }
        })
    MenuItem("Logout", onClick = { LoginManager.getInstance().logOut() })
  }
}

linmx0130 avatar Dec 23 '21 19:12 linmx0130

Thanks for the quick answer @linmx0130 ! I have something quite similar to your example, but my callbacks don't get called... I'm using DeviceLoginManager instead of LoginManager, but since it's extending it, I'm guessing that's not the cause.

Simplifying, I have this:

public static void Login()
{
	s_callbackManager = CallbackManager.Factory.create();
	RegisterLoginResponse();
	DeviceLoginManager.getInstance().logInWithReadPermissions((ActivityResultRegistryOwner) s_instance.m_activity, s_callbackManager, m_permissions);
}

public static void RegisterLoginResponse()
{
	if (s_loginCallback == null)
		{
			s_loginCallback = new FacebookCallback<LoginResult>()
			{
					@Override
					public void onSuccess(LoginResult loginResult)
					{
						//handle success
					}

					@Override
					public void onCancel()
					{
						//handle cancel
					}

					@Override
					public void onError(FacebookException exception)
					{
						//handle error
					}
			};
		}

	DeviceLoginManager.getInstance().registerCallback(s_callbackManager, s_loginCallback);
}

georgii11 avatar Dec 24 '21 11:12 georgii11

Hi @georgii11! Thanks for the report! However, we cannot reproduce the problem. Could you share us with a reproducible example? Thanks a lot!

linmx0130 avatar Jan 18 '22 18:01 linmx0130

Also have a similar issue with SDK 13.1.0 ShareDialog. But in my case not using Jetpack Compose.

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

    private suspend fun ShareDialog.awaitSharerResult(): Sharer.Result = suspendCancellableCoroutine { continuation ->
        registerCallback(facebookCallbackManager, object : FacebookCallback<Sharer.Result> {
            override fun onSuccess(result: Sharer.Result) = continuation.resume(result)
            override fun onError(error: FacebookException) = continuation.resumeWithException(error)
            override fun onCancel() = continuation.cancel().let { Unit }
        })
    }

Debugging shows onActivityResult is still hit, but the callbacks are never called. On older SDKs it works as expected where the callbacks are called immediately following onActivityResult.

https://developers.facebook.com/docs/sharing/android Followed the example on this page and unfortunately it seems to only apply for older SDK.

Exact same issue as georgii11.

I also tried removing the onActivityResult overrides per the comment: https://github.com/facebook/facebook-android-sdk/issues/954#issuecomment-925234490 above

mborahilly avatar Mar 23 '22 05:03 mborahilly

Hi all! I'm working on exposing the contract for login/sharing. I expect that it can be ready in v13.2.0. Thank you all for the discussion and the feedback!

linmx0130 avatar Mar 23 '22 23:03 linmx0130

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jun 22 '22 05:06 github-actions[bot]

Hi all! I'm working on exposing the contract for login/sharing. I expect that it can be ready in v13.2.0. Thank you all for the discussion and the feedback!

Was this released?

mborahilly avatar Jun 22 '22 16:06 mborahilly

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Sep 21 '22 05:09 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

github-actions[bot] avatar Sep 28 '22 05:09 github-actions[bot]