简体   繁体   中英

onActivityResult requestcode Facebook SDK

I'm building an Android app which uses the Facebook SDK so people can share URL's. I'm using onActivityResult for multiple things in my activity, so I'm using a switch on requestCode so that I know what to do with every activityresult. How do I get the proper requestCode when for example I cancel sharing a facebook post? This is my code at the moment:

import com.facebook.CallbackManager;

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case 1: 
        ... doing some not-facebook-related stuff here
        case 2:
        ... doing some other not-facebook-related stuff here


//      case ??? : 
//      callbackManager.onActivityResult(requestCode, resultCode, data);

So what is the right requestCode and how do I get this?

This is what I've used in a similar case:

    if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
        // call callbackManager
    }

You can also set the offset of requestcodes used by FacebookSdk:

FacebookSdk.sdkInitialize(getApplicationContext(), 10000);

By the way, I have found this looking through the source code, so I'm not sure how "safe" this option is as CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode() is not a "public" documented method.

Try using a sharedialog instead

new ShareDialog(this).registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });

When you cancel share you get to onCancel callback. Also uncomment this

callbackManager.onActivityResult(requestCode, resultCode, data);

from onActivityResult method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM