flutter_facebook_login icon indicating copy to clipboard operation
flutter_facebook_login copied to clipboard

Can't get result while use onActivityResult on MainActivity.kt

Open pmrajani opened this issue 5 years ago • 5 comments

Hi i am using flutter_facebook_login: ^1.2.0 for facebook login

Here is my code what i wrote on my flutter class

login.dart `Future<Null> initiateFacebookLogin() async {

final FacebookLoginResult result =
await facebookSignIn.logInWithReadPermissions(['email']);

switch (result.status) {
  case FacebookLoginStatus.loggedIn:
    final FacebookAccessToken accessToken = result.accessToken;
    print('''
     Logged in!
     
     Token: ${accessToken.token}
     User id: ${accessToken.userId}
     Expires: ${accessToken.expires}
     Permissions: ${accessToken.permissions}
     Declined permissions: ${accessToken.declinedPermissions}
     ''');
    break;
  case FacebookLoginStatus.cancelledByUser:
    print('Login cancelled by the user.');
    break;
  case FacebookLoginStatus.error:
    print('Something went wrong with the login process.\n'
        'Here\'s the error Facebook gave us: ${result.errorMessage}');
    break;
}

}`

Now in my App i am using Braintree for payment, that's why i used MethodChannel to make payment on Native so here is my MainActivity.kt

`class MainActivity : FlutterActivity(), BraintreeCancelListener, BraintreeErrorListener {

var TAG = "MainActivity";
var REQUEST_CODE = 111

lateinit var resReplay: MethodChannel.Result;
companion object {
    const val CHANNEL = "com.myapp.xyz"
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)

    MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
        if (call.method == "showNativeView") {
            val args = call.arguments as List<*>
            val param = args.first() as String
            Log.e("Native Call", "Native Call");
            onBraintreeSubmit(param);
            resReplay=result;

        }
    }
}

fun onBraintreeSubmit(token:String) {
    val dropInRequest = DropInRequest()
            .clientToken(token)
    startActivityForResult(dropInRequest.getIntent(this), REQUEST_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            val result = data.getParcelableExtra<DropInResult>(DropInResult.EXTRA_DROP_IN_RESULT)

            val nonce = result.paymentMethodNonce!!.nonce
            resReplay.success(nonce)

        } else if (resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(applicationContext, "the user canceled", Toast.LENGTH_SHORT).show()
        } else {
            val error = data.getSerializableExtra(DropInActivity.EXTRA_ERROR) as Exception
            Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT).show()

        }
    }
}

`

ISSUE is if i remove onActivityResult method from MainActivity, Facebook login works perfectly But if i can try to login with facebook with above code it's dosent respond any success or error log.

pmrajani avatar Apr 04 '19 13:04 pmrajani

Same Issue on my side also. @pmrajani did you get any solution ?

AhsanKhan231 avatar Apr 06 '19 20:04 AhsanKhan231

yes @AhsanKhan231 i fixed it.

pmrajani avatar Apr 09 '19 09:04 pmrajani

Not standard solution but i thought issue is due to OnActivityResult method on MainActivity.kt

Facebook plugin also called it internally. so i remove my MainActivity code on other class and facebook working for me.

pmrajani avatar Apr 09 '19 10:04 pmrajani

@pmrajani I have the same problem with the difference that I am not using native approach for Braintree - I am using plugins for Facebook, Google and Braintree, no native code added. What should I do to make it work then, do you have any ideas?

scourgy avatar Feb 13 '20 09:02 scourgy

By using this way I have solved the issue.

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY: if (resultCode == Activity.RESULT_OK && data != null) { String path = new FileUtils().getPathFromUri(this, data.getData()); if (result != null) { result.success(path); } this.result = null; } break; default: } }

arpans avatar Feb 16 '21 09:02 arpans