9

I am using following code to share link on facebook. when user click on cancel on Share dialog interface,onSuccess() callback method is called sometimes instead of onCancel(). And getting post id null.Please help me what's going wrong?

ShareButton btn;
CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this);
    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_share);

    btn = (ShareButton) findViewById(R.id.btn_share);

    btn.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            Log.e("Tag","Successfully posted");
            Log.e("Post id",result.getPostId());
        }

        @Override
        public void onCancel() {

            Log.e("Tag","Canceled by user");

        }

        @Override
        public void onError(FacebookException error) {

            Log.e("Tag",error.getLocalizedMessage());
        }
    });
    ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse("My Custom URL"))
            .setContentTitle("Test")
            .build();

    btn.setShareContent(content);

}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
Harshal Bhatt
  • 731
  • 10
  • 25

1 Answers1

3

Well, maybe I'm late but I faced the same issue weeks ago. What I've noticed is if you press Cancel in the web-base Share dialog, the onSuccess() method is called but the Share.Result object contains a null postId, so you can control whenever the user pressed cancel or share by checking the Share.Result response.

Another thing I've noticed is that if you share content with native app installed, postId field is always null... so you will have to check if the user has the app installed to check or not the postId field.

josliber
  • 43,891
  • 12
  • 98
  • 133
ablanco
  • 41
  • 4
  • I tried using the `PostId` to identify a success post action but as you said when the native app is installed the `PostId` is always null. Do you have any solution to detect cancel in this scenario? – rareyesdev Oct 19 '15 at 17:16
  • Just check if the user has Facebook package installed to do one or other logic. – ablanco Oct 21 '15 at 17:35
  • This solution fails when user is installed Facebook native app and cancels the sharing by clicking the back arrow button on top-left side of the dialog, in this case onSuccess is called again, with postId being null. – NecipAllef Nov 09 '15 at 08:03