Search code examples
androidfacebooksharefacebook-sdk-4.0facebook-share

Android Facebook sharing is opening shareDialog twice


I'm using the Facebook ShareDialog to share links and photos, depending on the content. But I'm seeing a strange issue. When I click on the facebook icon in my shareActionProvider, it first opens the ShareDialog with a blank post. Then, when I click to go back to my application, it re-opens the ShareDialog with the link/ photo content I wanted to show.

Here is the code I'm using to share.

....

shareActionProvider.setOnShareTargetSelectedListener(new MyActionProvider.OnShareTargetSelectedListener() {
       @Override
       public boolean onShareTargetSelected(MyActionProvider source, Intent intent) {
           // Recover selected application name for custom action handling
          final String appName = intent.getComponent().getPackageName();

          switch (appName) {
              case "com.facebook.katana":                 // Facebook

                        ShareLinkContent content = new ShareLinkContent.Builder()
                                .setContentUrl(Uri.parse("https://developers.facebook.com"))
                                .setContentTitle("Check it out!")
                                .build();

                        shareDialog.show(content);

                        break;

               ....
          }

Has anyone seen this behavior before?

Thanks!!

I've attached both screens that I'm seeing, in order that I'm seeing them in.

First screen

Second dialog screen

EDIT

I'm adding more of my code.

I'm sharing to FB using a custom ShareActionProvider (which is copied exactly from the Android source code, except for me overriding the setOnShareTargetSelectedListener)

In my Activity onCreate:

FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareDialog.registerCallback(
            callbackManager,
            shareCallback);

When I set up my ShareActionProvider:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");

if (shareActionProvider != null) {
    shareActionProvider.setShareIntent(shareIntent);
    //... this is before the setOnShareTargetSelected call ....

Solution

  • Ok I found the solution.

    Since I was setting my shareIntent before calling the Facebook ShareDialog, it was being called twice.

    In my custom ShareActionProvider, I had overridden the ShareActivityChooserModelPolicy class. It looked like this:

    /**
     * Policy that delegates to the {@link OnShareTargetSelectedListener}, if such.
     */
    private class ShareActivityChooserModelPolicy
            implements MyActivityChooserModel.OnChooseActivityListener {
        @Override
        public boolean onChooseActivity(MyActivityChooserModel host, Intent intent) {
            if (mOnShareTargetSelectedListener != null) {
                mOnShareTargetSelectedListener.onShareTargetSelected(MyActionProvider.this, intent);
            }
            return false;
        }
    }
    

    From this post: This class calls the listener, but then returns false regardless. Returning true from this method would allow us to handle the intent, without invoking the default behaviour.

    So all I did was change

    mOnShareTargetSelectedListener.onShareTargetSelected(MyActionProvider.this, intent);
    

    to

    return mOnShareTargetSelectedListener.onShareTargetSelected(MyActionProvider.this, intent);
    

    And added return true after calling shareDialog.show(content)

    That solved my issue.

    Hope it helps others!