13

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);
}
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
Paul Freez
  • 1,059
  • 15
  • 27

4 Answers4

31

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);
}
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
  • 1
    Nope, adding super() call didn't fix anything - still no effect – Paul Freez Apr 07 '15 at 11:26
  • 1
    where did you put that ? on fragment or activity ? – Heshan Sandeepa Apr 07 '15 at 11:30
  • Fragment. Everything is in my Fragment. – Paul Freez Apr 07 '15 at 11:40
  • 4
    put that on hosting activity – Heshan Sandeepa Apr 07 '15 at 11:43
  • Wow, didn't actually expect that working, thanks! It's a little bit strange because I user almost same callback on another Fragment of mine and the callback was called. BTW - I've also removed my shareDialog.show(f, composeContent(title, description, link)); method to shareDialog.show(composeContent(title, description, link)); cause it seems that I was using static method. – Paul Freez Apr 07 '15 at 12:01
  • Yea.. I made same mistake. Keeping `OnActivityResult()` didn't work in Fragment. But keeping it on hosting Activity made my day.. :) Thanks... – Chintan Soni Jan 08 '16 at 08:37
1

you need to specify A requestCode when you register a facebookcallback. shareDialog.registerCallback(**, **, requestCode);

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
Lincoln
  • 11
  • 1
1

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);
Hulk
  • 2,565
  • 16
  • 24
0

Make sure you pass the fragment (instead of passing the activity) when you create the ShareDialog:

// From within a fragment
ShareDialog dialog = new ShareDialog(this);

Then you can leave the facebookCallbackManager.onActivityResult() inside your fragment's onActivityResult() method.

mVck
  • 2,910
  • 2
  • 18
  • 20