I have added sharing functionality to Android app as described here https://developers.facebook.com/docs/android/share-dialog/#setup
But I have noticed that if user is cancelled sharing activity onComplete
is called anyway
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.e("Activity", String.format("Error: %s", error.toString()));
}
@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.e("Activity", "Success!");
}
});
}
I have also looked in to Bundle which is returned. Even if I cancel share dialog I get
com.facebook.platform.extra.DID_COMPLETE=true
How can I get result that user really shared data on facebook? (Without making separate login with facebook button. Maybe some permissions need to be added?)
In order to obtain the result for the sharing, your app needs to have at least the basic_info permission.
To solve that, just open an session (this will automatically request the basic_info permission):
You can find more information in here: https://developers.facebook.com/docs/android/getting-started/
See https://developers.facebook.com/docs/android/share-dialog/#handling-responses
You can tell if the user has cancelled by calling
where data is the result bundle. However, it will only return a non-null value IF the user has logged in via your app (i.e. you have at least basic_info permissions). If the user has never logged in or authorized your app, then the only thing you'll see is the DID_COMPLETE, and it will always be true unless an error occurred. This is by design.
the value of
data.getString("com.facebook.platform.extra.COMPLETION_GESTURE")
is "post" when the user did post on Facebook.Implement
FacebookCallback<Sharer.Result>
to know whether sharing was successful or cancelled or there was an error.You can use the code below in Activity and in Fragment as well. When using in Fragment make sure you pass
this
in ShareDialog constructor. If you passgetActivity()
thenonActivityResult
method will not be triggered in Fragment.Use this code:-
Heading ## private static ShareDialog shareDialog;