Dialog dialog not available

I have the following code for my sharing dialog inside the fragment:

TabFour.java

public class TabFour extends Fragment { private UiLifecycleHelper uiHelper; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_tab_four, container, false); return rootView; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); uiHelper = new UiLifecycleHelper(getActivity(), callback); uiHelper.onCreate(savedInstanceState); OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class); action.setProperty("book", "https://example.com/book/Snow-Crash.html"); @SuppressWarnings("deprecation") FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(getActivity(), action, "books.reads", "book") .build(); uiHelper.trackPendingDialogCall(shareDialog.present()); } @Override public 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.i("Activity", "Success!"); } }); } @Override public void onResume() { super.onResume(); uiHelper.onResume(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); uiHelper.onSaveInstanceState(outState); } @Override public void onPause() { super.onPause(); uiHelper.onPause(); } @Override public void onDestroy() { super.onDestroy(); uiHelper.onDestroy(); } private Session.StatusCallback callback = new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { // TODO Auto-generated method stub } }; 

}

Facebok Share downloads the emulator, however it does not work on the phone. Also, when the emulator is loaded on the "Sharing" tab, you cannot use it because the sharing button in the upper left corner is disabled (visible, but cannot be pressed). In addition, the emulator displays a toast message "Preview cannot be selected." Please, help!

Hope someone can explain this to me. Thanks!

+6
source share
1 answer

From https://developers.facebook.com/docs/android/share

The facebook sharing dialog only works if the facebook application is installed. Thus, the proposed method is to first check whether it is possible to provide the Share dialog, and if presentable, present it with your content or use the web dialog to present the shared screen. i.e

 if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) { // Publish the post using the Share Dialog FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this) .setLink("https://developers.facebook.com/android") .build(); uiHelper.trackPendingDialogCall(shareDialog.present()); } else { // Fallback. For example, publish the post using the Feed Dialog } 

And here is an example of the else option

 private void publishFeedDialog() { Bundle params = new Bundle(); params.putString("name", "Facebook SDK for Android"); params.putString("caption", "Build great social apps and get more installs."); params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps."); params.putString("link", "https://developers.facebook.com/android"); params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); WebDialog feedDialog = ( new WebDialog.FeedDialogBuilder(getActivity(), Session.getActiveSession(), params)) .setOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(Bundle values, FacebookException error) { if (error == null) { // When the story is posted, echo the success // and the post Id. final String postId = values.getString("post_id"); if (postId != null) { Toast.makeText(getActivity(), "Posted story, id: "+postId, Toast.LENGTH_SHORT).show(); } else { // User clicked the Cancel button Toast.makeText(getActivity().getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT).show(); } } else if (error instanceof FacebookOperationCanceledException) { // User clicked the "x" button Toast.makeText(getActivity().getApplicationContext(), "Publish cancelled", Toast.LENGTH_SHORT).show(); } else { // Generic, ex: network error Toast.makeText(getActivity().getApplicationContext(), "Error posting story", Toast.LENGTH_SHORT).show(); } } }) .build(); feedDialog.show(); } 

And don't always rely on an emulator to check things out. After all, this is just an "emulator". You can easily view your application on a real device. Therefore, if something does not work, as expected, on the emulator, check it on a real device. (Of course, I agree, the emulator is good when you do not have multiple devices, screen sizes, etc.)

PS: You also need your hash files to be correctly installed on the application settings page on the fb developers website.

0
source

Source: https://habr.com/ru/post/956389/


All Articles