Why doesn't Android Facebook interface work with Fragments?

前端 未结 5 548
粉色の甜心
粉色の甜心 2021-01-07 05:21

I\'m switching some Android Facebook code from an Activity to a Fragment. Prior to the switch everything worked fine, but now the

5条回答
  •  鱼传尺愫
    2021-01-07 06:04

    Facebook SDK don't works with Fragment, but works with FragmentActivity. So you need:

    1. Catch the FB login info in onActivityResult() in your parent FragmentActivity.
    2. Pass activity results to your child Fragment

    example:

    1.

    public class MainActivity extends FragmentActivity(){
    
    
    /* catch FACEBOOK login info and call onFBLoginActivityResult() to pass it to FacebookFrragment*/
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
           android.support.v4.app.FragmentManager fm =   getSupportFragmentManager();
           FacebookFragment fbfragment= (FacebookFragment ) fm.findFragmentByTag("facebookfragment");
           selectPKEConfigFragment.onFBLoginActivityResult(requestCode, resultCode, data);
    
        }
    
    }
    

    2.

    public class FacebookFragment extends Fragment{
    
            @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                    Bundle savedInstanceState) {
            // ........... 
    
             // start Facebook Login
    
                        Session.openActiveSession(getActivity(), true, new Session.StatusCallback() {
    
                          // callback when session changes state
                          @Override
                          public void call(Session session, SessionState state, Exception exception) {
                            if (session.isOpened()) {
                              // make request to the /me API
                              Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
    
                                // callback after Graph API response with user object
                                @Override
                                public void onCompleted(GraphUser user, Response response) {
                                  if (user != null) {
                                   // TextView welcome = (TextView) findViewById(R.id.welcome);
                                   // welcome.setText("Hello " + user.getName() + "!");
                                      Log.i(TAG,"User: " + user.getName());
                                  }
                                  else  Log.i(TAG,"User: null");
                                }
                              });
                            }
                            else Log.i(TAG,"session closed");
                          }
    
    
                        });
    
                }
    
            /*
             * replace onActivityResult
             */
            public void onFBLoginActivityResult(int requestCode, int resultCode, Intent data){
                 Log.i(TAG,"Activity result SelectPKEConfig");
                Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
            }
    

提交回复
热议问题