FB Share button working incorrectly

I am trying to implement the FB Share button in my Android app with latest FB SDK 4.4.1
I have followed all steps given on Facebook developer page:
https://developers.facebook.com/docs/sharing/android

Only on successful posting of the content, I want to call a certain function func1().

When I tap on FB Share button, it takes me to native FB app.

What is happening:

  1. When I tap back button, it returns to my app, onSuccess() is called with result.getPostId() = null.
  2. When I post on my timeline, it returns to my app, onSuccess() is called with result.getPostId() = null.

What should happen:

  1. When I tap back button, it should return to my app, onCancel() shall be called.
  2. When I post on my timeline, it should return to my app, onSuccess() shall be called with result.getPostId() != null.

Surprisingly, when I tried this on a phone which doesn’t have FB native app installed, the FB web interface is shown and onSuccess() and onCancel() are called correctly.

Give below code:

FBShare.java

public class FBShare extends Activity {
private ShareButton shareButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fb_share);
    callbackManager = CallbackManager.Factory.create();
    shareButton = (ShareButton)findViewById(R.id.shareButton);
    shareButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ShareLinkContent linkContent = new ShareLinkContent.Builder()
            .setContentTitle("My App")
            .setContentDescription("My Desc")
            .setContentUrl(Uri.parse("https://myapp.net"))
            .setImageUrl(Uri.parse("https://myapp.net/12.png"))
            .build();

            shareButton.setShareContent(linkContent);
        }
    });
    shareButton.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Result result) {
            Log.i("FB Share button","success");
            if (result.getPostId()!=null){
                func1();
            }
        }

        @Override
        public void onCancel() {
            Log.i("FB Share button","cancel");
        }

        @Override
        public void onError(FacebookException error) {
            Log.i("FB Share button","error");
        }
    });
}

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

fb_share.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/shareButton"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:paddingTop="10dp"
    android:text="@string/fb_share_text"
    android:textColor="@color/black"
    android:textStyle="italic|bold" />

<com.facebook.share.widget.ShareButton
    android:id="@+id/shareButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

Anyone can help me? Am I doing something wrong?


Source: facebook

Leave a Reply