Sharing callback doesnt work wiith Facebook SDK 4 Android
Asked Answered
O

4

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);
}
Ozenfant answered 7/4, 2015 at 7:21 Comment(1)
post your whole activity / fragment pleaseHowever
H
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);
}
However answered 7/4, 2015 at 11:18 Comment(6)
Nope, adding super() call didn't fix anything - still no effectOzenfant
where did you put that ? on fragment or activity ?However
Fragment. Everything is in my Fragment.Ozenfant
put that on hosting activityHowever
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.Ozenfant
Yea.. I made same mistake. Keeping OnActivityResult() didn't work in Fragment. But keeping it on hosting Activity made my day.. :) Thanks...Nitwit
D
1

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

Dimity answered 26/8, 2015 at 8:13 Comment(0)
C
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);
Crowberry answered 20/2, 2016 at 11:38 Comment(0)
T
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.

Tuberculosis answered 25/4, 2017 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.