2

我正在使用 Facebook sdk 3,并且我有一个带有共享按钮的片段。
乍一看,它有效。
在第二次通话中,我得到了片段与活动分离
06-24 10:24:47.430: W/FragmentActivity(2812): Activity result no fragment exists for index: 0x3face
之后onActivityResult,我看到了该活动的前一个片段。

这是我的代码:

public class AboutFragment extends BaseFragment implements OnClickListener, Session.StatusCallback {

    private static final String GA_CATEGORY = "About";
    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");

    private Button mShareEmailButton;
    private Button mShareFacebookButton;
    private Button mConatctUsButton;
    private WebView mAboutText; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View v = inflater.inflate(R.layout.fragment_about, container, false);

        mShareEmailButton = (Button) v.findViewById(R.id.buttonShareEmail);
        mShareFacebookButton = (Button) v.findViewById(R.id.buttonShareFacebook);
        mConatctUsButton = (Button) v.findViewById(R.id.buttonContactUs);
        mAboutText = (WebView) v.findViewById(R.id.webViewAbout);

        mAboutText.loadUrl("file:///android_asset/about.html"); 

        mShareEmailButton.setOnClickListener(this);
        mShareFacebookButton.setOnClickListener(this);
        mConatctUsButton.setOnClickListener(this);

        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonShareEmail:
                GA_Handler.sendEvent(getActivity(), GA_CATEGORY, "Share_click", "Email");
                shareViaEmail();
                break;

            case R.id.buttonShareFacebook:
                GA_Handler.sendEvent(getActivity(), GA_CATEGORY, "Share_click", "FB");
                checkFacebookLogin();
                break;

            case R.id.buttonContactUs:
                GA_Handler.sendEvent(getActivity(), GA_CATEGORY, "Contact_us_click");
                ((BaseNavigationActivity)getActivity()).loadFragment(new ContactUsFragment());
                break;

            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        boolean isFacebookResponse =
                Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
        if (isFacebookResponse) {
          System.out.println("FB Response");
        }
    }

    /**
     * Login to FB if needed
     */
    public void checkFacebookLogin() {

        try {
            logInToFacebook();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    private void logInToFacebook() {
        String app_id = getString(R.string.app_id);

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        Session session = new Session.Builder(getActivity().getApplicationContext())
                .setApplicationId(app_id)
                .build();
        session.addCallback(this);
        Session.setActiveSession(session);

        // Login
        if (!session.isOpened() && !session.isClosed()) {
            session.openForPublish(new Session.OpenRequest(this)
                    .setPermissions(PERMISSIONS)
                    .setCallback(this));
        } else {
            Session.openActiveSession(getActivity(), true, this);
        }
    }

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        System.out.println("ABOUT:  " + state);
        if (state == SessionState.OPENED) {
            if(isAdded()){
                publishFeedDialog();
            }else{
                System.out.println("Home activity not attached");
            }
        }
    }

    /**
     * Publish to FB
     */
    private void publishFeedDialog() {
        Bundle params = new Bundle();
        params.putString("name", getString(R.string.fb_share_name));
        params.putString("caption", getString(R.string.fb_share_caption));
        params.putString("description", getString(R.string.fb_share_description));
        params.putString("link", getString(R.string.fb_share_link));
        params.putString("picture", getString(R.string.fb_share_picture));

        WebDialog feedDialog = (
                new WebDialog.FeedDialogBuilder(getActivity(),
                        Session.getActiveSession(), params)) //
                .setOnCompleteListener(new WebDialog.OnCompleteListener() {

                    Context appContext = getActivity().getApplicationContext();

                    @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) {
                                DebugToast.show(appContext, "Posted story, id: " + postId);
                            } else {
                                // User clicked the Cancel button
                                DebugToast.show(appContext, "Publish cancelled");
                            }
                        } else if (error instanceof FacebookOperationCanceledException) {
                            // User clicked the "x" button
                            DebugToast.show(appContext, "Publish cancelled");
                        } else {
                            // Generic, ex: network error
                            DebugToast.show(appContext, "Error posting story");
                        }

                        logOut();
                    }
                })
                .build();
        feedDialog.show();
    }

    /**
     * Disconnect from facebook
     */
    public void logOut() {
        Session session = Session.getActiveSession();
        if (!session.isClosed()) {
            session.closeAndClearTokenInformation();
        }
    }
}

如何使其正常工作?
也接受了不同的解决方案
在此先感谢

4

0 回答 0