Android facebook 4.0.0 share dialog does not share the content

gabby picture gabby · Mar 27, 2015 · Viewed 13.8k times · Source

For hours I have been trying to share a content in my android app by facebook 4.0.0 sdk. I exactly followed facebook share document but got no result. Share dialog open when I press share button but no content is in it. If I click ok it share an empty string just. Please show me a way to fix this.

Edit

BTW When I remove Facebook native app from my phone, I can share with webview of facebook.

CallbackManager callbackManager;
    ShareDialog shareDialog;
    private View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(activity);
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog(activity);
        rootView = inflater.inflate(R.layout.fragment_events, container, false);

        shareDialog.registerCallback(callbackManager, shareCallBack);

        lbtnShare        = (LinearLayout) rootView.findViewById(R.id.lbtn_shareOnFb); 
        lbtnShare.setOnClickListener(this);

        init(rootView);
        return rootView;
    }

public FacebookCallback<Sharer.Result> shareCallBack = new FacebookCallback<Sharer.Result>() {

        @Override
        public void onSuccess(Result result) {
            showToast(message(R.string.title_fbShare)).show();
        }
        @Override
        public void onCancel() {
        }
        @Override
        public void onError(FacebookException error) {
            showToast(message(R.string.msgerr_shareOnFB) + " -- " + error.getMessage()).show();
        }
    };

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.lbtn_shareOnFb: 
            shareOnFB();
            break;
        default:
            break;
        }
    }

private void shareOnFB(){
        if (ShareDialog.canShow(ShareLinkContent.class)) {
            String eventUrl = "http://www.mypage.com?id=" + event.getId();
            eventUrl = eventUrl.replaceAll(" ", "-");

            ShareLinkContent adShareContent = new ShareLinkContent.Builder()
                                .setContentTitle(event.getTitle())
                                .setContentDescription(message(R.string.fbshareDesc))
                                .setContentUrl(Uri.parse(eventUrl))
                                .setImageUrl(Uri.parse(event.getImages().get(0).getName())).build();

            shareDialog.show(adShareContent);
        }
    }

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

enter image description here

Answer

Gokhan Caglar picture Gokhan Caglar · Mar 28, 2015

You need to create the ShareDialog with the fragment you are in(this), not the activity. The onActivityResult is returned to the activity as it's written so it doesn't make it to the callback manager.