Facebook requestCodes

前端 未结 7 743
予麋鹿
予麋鹿 2021-02-02 11:26

I have an Activity that should handle results from both the Facebook SDK, and from other custom Activities.

Where can I find the requestCodes used by the Facebook SDK, i

相关标签:
7条回答
  • 2021-02-02 12:03

    Pass the request code in the sdkInitialize call

    FacebookSdk.sdkInitialize(context, 1200);
    

    Then

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (FacebookSdk.isFacebookRequestCode(requestCode)) {
          //Facebook activity result
          //Do your stuff here
          //Further you can also check if it's login or Share etc by using 
          //CallbackManagerImpl as explained by rajath's answer here
          if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
              //login
          }
          else if (requestCode == CallbackManagerImpl.RequestCodeOffset.Share.toRequestCode()){
             //share
          }
    }
    

    From the docs

    isFacebookRequestCode(int)

    Returns true if the request code is within the range used by Facebook SDK requests. This does not include request codes that you explicitly set on the dialogs, buttons or LoginManager. The range of request codes that the SDK uses starts at the callbackRequestCodeOffset and continues for the next 100 values.

    sdkInitialize(Context, int)

    This function initializes the Facebook SDK, the behavior of Facebook SDK functions are undetermined if this function is not called. It should be called as early as possible.

    public static synchronized void sdkInitialize(Context applicationContext, int callbackRequestCodeOffset)
    applicationContext The application context
    callbackRequestCodeOffset The request code offset that Facebook activities will be called with. Please do not use the range between the value you set and another 100 entries after it in your other requests.

    0 讨论(0)
  • 2021-02-02 12:07

    Try this:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
    
        //Log.iClassMethod();
    
        switch(requestCode)
        {
            case 1:
                if (resultCode == Activity.RESULT_OK)
                {
                    // do something ...
                }
                break;
    
            case ...:
                if (resultCode == Activity.RESULT_OK)
                {
                    // do something ...
                }
                break;
    
            case Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE:
            {
                Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
                Log.i("Facebook");
            }
            break;
        }
    }
    
    0 讨论(0)
  • 2021-02-02 12:17

    Adding to Apetroaei Andrei's answer, there are other options available at Facebook SDK's CallbackManagerImpl class:

    public enum RequestCodeOffset {
        Login(0),
        Share(1),
        Message(2),
        Like(3),
        GameRequest(4),
        AppGroupCreate(5),
        AppGroupJoin(6),
        AppInvite(7),
        ;
    

    These can be accessed by the foll. code:

    if (requestCode == CallbackManagerImpl.RequestCodeOffset.Share.toRequestCode()) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    
    0 讨论(0)
  • 2021-02-02 12:22

    Offering an alternative if you're using FB login via LoginButton

    1. Set request code of login button
    2. Use the request code to differentiate activity

    private LoginButton mFacebookLoginButton;
    private static int RC_FB_SIGN_IN;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ...
       mFacebookLoginButton = (LoginButton) findByViewId(R.id.fb_login_button);
       mFacebookLoginButton.registerCallback(...)
       RC_FB_SIGN_IN = mFacebookLoginButton.getRequestCode();
       ...
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_GP_SIGN_IN) {
            ...
        } else if (requestCode == RC_FB_SIGN_IN) {
            Log.i(TAG, "Doing my facebook usual things");
            mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }
    
    0 讨论(0)
  • 2021-02-02 12:23

    You can set your own request codes to disambiguate. All the OpenRequest and NewPermissionsRequest classes take a requestCode parameter:

    setRequestCode

    0 讨论(0)
  • 2021-02-02 12:25

    Go to CallbackManagerImpl.RequestCodeOffset. I personally used a piece of code like this to prevent unwanted behaviour.

    if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    
    0 讨论(0)
提交回复
热议问题