Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook requestCodes

Tags:

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, in order to not use the same for my Activities?

I should be able to tell them apart in the onActivityResult using their requestCode so they need to be unique.

like image 209
MasterScrat Avatar asked Jul 21 '13 13:07

MasterScrat


People also ask

What is CallbackManager?

The CallbackManager manages the callbacks into the FacebookSdk from an Activity's or Fragment's onActivityResult() method. Nested Class Summary. Modifier and Type. Class.


2 Answers

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.

like image 172
Inder Kumar Rathore Avatar answered Nov 03 '22 02:11

Inder Kumar Rathore


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);     } 
like image 33
Apetroaei Andrei Avatar answered Nov 03 '22 03:11

Apetroaei Andrei