Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing callback doesnt work wiith Facebook SDK 4 Android

I've recently moved my app to FB SDK 4.0 and I got some weird troubles with sharing. The sharing dialog works great - I'm able to share with both Facebook app and WebDialog. However, after successful/failure sharing my callback doesn't work at all - so I can't even show a toast or log anything. Here's how I do:

shareDialog.registerCallback(fbManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                // This doesn't work
                Toast.makeText(f.getActivity(), "You shared this post", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {
            }

            @Override
            public void onError(FacebookException e) {
                // This doesn't work
                e.printStackTrace();
            }
        });

I've even tried debugging the app - no effect - this code wasn't even called.

So could you point me at what am I doing wrong or what am I missing?

Update

Since I'm using special class for working with FB SDK, here's the part of it:

private static CallbackManager fbManager;

public static CallbackManager init(Activity c) {
    if (!FacebookSdk.isInitialized()) {
        FacebookSdk.sdkInitialize(c);
    }

    return fbManager = CallbackManager.Factory.create();
}

 ...

 public static void share(final Fragment f, final String title, final String description, final String link) {
    ShareDialog shareDialog = new ShareDialog(f);
    shareDialog.registerCallback(fbManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            Toast.makeText(f.getActivity(), "You shared this post", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException e) {
            e.printStackTrace();
        }
    });
    shareDialog.show(f, composeContent(title, description, link));

And here how it looks like in fragment:

 private CallbackManager callbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    callbackManager = FacebookHelper.init(getActivity());
}

...

  @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

 private void publishStory() {
    FacebookHelper.share(this,
            "Title",
            getResources().getString(R.string.sharing_text),
            sharingLink);
}
like image 586
Paul Freez Avatar asked Apr 07 '15 07:04

Paul Freez


2 Answers

You have to override the onActivityResult method, Update your method as follows,

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  callbackManager.onActivityResult(requestCode, resultCode, data);
}
like image 199
Heshan Sandeepa Avatar answered Sep 26 '22 06:09

Heshan Sandeepa


Don't forget to override:

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

If using in Fragment, then also must add:

loginBtn = (LoginButton) view.findViewById(R.id.fb_login_button);
loginBtn.setFragment(this);
like image 25
Hulk Avatar answered Sep 26 '22 06:09

Hulk