Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Android SDK v2.3 Unexpected call to LoginManager.onActivityResult

<< EDITED >>
I'm trying to upgrade the facebook SDK in my android app. I'm doing it following the docs on the facebook developers site. I want to make to slight modifications

1) I want to make the login from a DialogFragment not directly in the activity
2) I want to do the Facebook login using my own button

This is what my activity look like:

public class MainActivity extends ActionBarActivity {
    private static final String TAG = MainActivity.class.getName();

    private CallbackManager mFBCallbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Setup FB SDK add login callback
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        mFBCallbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(mFBCallbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "FB Login successfull");
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "FB Login cancelled");
            }

            @Override
            public void onError(FacebookException e) {
                Log.d(TAG, "Error while performing FB login", e);
            }
        });

        // Open login dialog
        Button loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new LoginDialog().show(getSupportFragmentManager(), TAG);
            }
        });
    }

    @Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // foward onActivityResult call to the FB callback manager
        mFBCallbackManager.onActivityResult(requestCode, requestCode, data);
    }
}

And this is what my DialogFragment looks like:

public class LoginDialog extends DialogFragment {

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View rootView = inflater.inflate(R.layout.login_dialog, null);

        Button fbLoginBtn = (Button) rootView.findViewById(R.id.fb_login_btn);
        fbLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginManager.getInstance().logInWithReadPermissions(
                        getActivity(),
                        Arrays.asList("basic_info"));
            }
        });

        builder.setView(rootView);
        return builder.create();
    }
}

I think the setup looks like what the Facebook mentions as required steps however I'm my FacebookCallback.onError(FacebookException) is getting called showing this on the logcat. Specific message on the exception "Unexpected call to LoginManager.onActivityResult"

04-03 12:01:26.390    8229-8229/com.jpdevs.fbv23 D/com.jpdevs.fbv23.MainActivity﹕ Error while performing FB login
    Unexpected call to LoginManager.onActivityResult
            at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:187)
            at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
            at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
            at com.jpdevs.fbv23.MainActivity.onActivityResult(MainActivity.java:65)
            at android.app.Activity.dispatchActivityResult(Activity.java:6135)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

If you would like to more details I created this repo with the whole project: https://github.com/jorgep19/fb_login

like image 309
Chinox.19 Avatar asked Apr 03 '15 03:04

Chinox.19


1 Answers

I had this problem and you will do a facepalm when i tell you the "secret". I will explain why this raise, if you just want the solution go to the end.

The source code of facebook throw this error when this happens:

if (exception == null && newToken == null && !isCanceled) {
    exception = new FacebookException("Unexpected call to LoginManager.onActivityResult");
}

So something is happening with this 3 params.

I was reading the code and i see that could be some problem with the resultCode because it's not returning an Activity.RESULT_OK.

THE KEY: You have passed the requestCode twice, on the resultCode param too... Just change this for this:

mFBCallbackManager.onActivityResult(requestCode, requestCode, data);

by

mFBCallbackManager.onActivityResult(requestCode, resultCode, data);
like image 73
Sulfkain Avatar answered Sep 21 '22 02:09

Sulfkain