Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK - Share dialog does not respond to cancel callback

I'm working with Facebook sdk 4.0 on my android app and I found this problem:

-when I share a post, I can see the facebook interface and I can post it and cancel it perfectly. I registered the callbacks, but if I press the cancel button, the onCancel callback is not called, the post is not published, but the onSuccess callback is called. However, if I touch the close button, everything works ok.

Here is my code:

 private void fbOnShare(){

    ShareLinkContent shareContent = new ShareLinkContent.Builder()
            .setContentTitle("The Simpson!")
            .setContentUrl(Uri.parse("http://www.foxplay.com/ar/show/6980-los-simpson?gclid=CPa-7N-y7MUCFYMSHwodNLYAKQ"))
            .build();


    this._btnShare = (ShareButton)findViewById(R.id.share_button);
    this._btnShare.setShareContent( shareContent );


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

            Log.v("MyApp", "Share success!"); //Showed if I press the share or the cancel button

        }

        @Override
        public void onCancel() {
            Log.v("MyApp", "Share canceled"); //Only showed when I press the close button
        }

        @Override
        public void onError(FacebookException e) {
            Log.v("MyApp","Share error: " + e.toString());
        }

    });
}

The _fbCallbackManager is created in another method that initializes everything in the onCreate method from the activity.

http://imgur.com/VbMee31

The "Cancelar" button only respond to the onSuccess callback inspite of does not post the share content. The "close" button works ok.

like image 200
AlphaDeveloper Avatar asked Feb 09 '23 23:02

AlphaDeveloper


2 Answers

According to THIS it is not a bug and it should stay that way.

The only way to know if the user accepted the publishing is to check for the postID on the result.

You will only have a postID if the user didn't cancel AND if the user is logged in to your app AND it has publish permissions. Otherwise it will always be null.

Yeah I know, it sucks.

like image 121
Dpedrinha Avatar answered Feb 13 '23 07:02

Dpedrinha


private boolean hasPublishPermission()
    {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();

        return accessToken != null && accessToken.getPermissions().contains("publish_actions");
    }

private void postStatusUpdate()
    {
        if(hasPublishPermission())
        {
            Log.d("PostStatus", "Ist");
            Profile profile = Profile.getCurrentProfile();
            ShareLinkContent linkContent = new ShareLinkContent.Builder()
            .setContentTitle("Hello Facebook")
            .setContentDescription( "The 'Hello Facebook' sample  showcases simple Facebook integration")
            .setContentUrl(Uri.parse("http://developers.facebook.com/docs/android"))
            .build();
            if(ShareDialog.canShow(ShareLinkContent.class))
            {
                Log.d("PostStatus", "2nd");
                ShareDialog shareDialog = new ShareDialog(instance);
                shareDialog.registerCallback(callbackManager, shareCallback);

            }
            else if (profile != null) 
            {
                Log.d("PostStatus", "3rdt");
                ShareApi.share(linkContent, shareCallback);
            } 
        }
        else
        {
            LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
        }

    }



private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>()
                    {

                @Override
                public void onSuccess(Result result) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onError(FacebookException error) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onCancel() {
                    // TODO Auto-generated method stub

                }
                    };
like image 23
Singhak Avatar answered Feb 13 '23 05:02

Singhak