Using share dialog in Android Facebook SDK. How to know is user actually shared or cancelled sharing activity?

Elizaveta  Ivanova picture Elizaveta Ivanova · Nov 2, 2013 · Viewed 9.8k times · Source

I have added sharing functionality to Android app as described here https://developers.facebook.com/docs/android/share-dialog/#setup

But I have noticed that if user is cancelled sharing activity onComplete is called anyway

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.e("Activity", String.format("Error: %s", error.toString()));
        }

        @Override
        public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
            Log.e("Activity", "Success!");
        }
    });
}

I have also looked in to Bundle which is returned. Even if I cancel share dialog I get

com.facebook.platform.extra.DID_COMPLETE=true

How can I get result that user really shared data on facebook? (Without making separate login with facebook button. Maybe some permissions need to be added?)

Answer

Ming Li picture Ming Li · Nov 4, 2013

See https://developers.facebook.com/docs/android/share-dialog/#handling-responses

You can tell if the user has cancelled by calling

String gesture = FacebookDialog.getNativeDialogCompletionGesture(data);
if (gesture != null) {
  if ("post".equals(gesture)) {
    // the user hit Post
  } else if ("cancel".equals(gesture)) {
    // the user hit cancel
  } else {
    // unknown value
  }
} else {
  // either an error occurred, or your app has never been authorized
}

where data is the result bundle. However, it will only return a non-null value IF the user has logged in via your app (i.e. you have at least basic_info permissions). If the user has never logged in or authorized your app, then the only thing you'll see is the DID_COMPLETE, and it will always be true unless an error occurred. This is by design.