2016-09-22 120 views
0

我有一個Android應用程序與facebook登錄in.I初始化Facebook的sdk在fragment.But onActivityResult從未被稱爲啓動登錄程序時。onActivityResult沒有在Facebook上登錄登錄

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     FacebookSdk.sdkInitialize(getContext()); 
     AppEventsLogger.activateApp(getContext()); 
     FacebookSdk.setApplicationId(getResources().getString(R.string.facebook_app_id)); 
     callbackManager = CallbackManager.Factory.create(); 
     View view=inflater.inflate(R.layout.fragment_signin, container, false); 
     gButton=(Button)view.findViewById(R.id.loginGmail); 
     fButton=(LoginButton) view.findViewById(R.id.loginFacebook); 
     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestEmail() 
       .build(); 
     fButton.setReadPermissions("email"); 
     gButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       signIn(); 
      } 
     }); 
     mGoogleApiClient = new GoogleApiClient.Builder(getContext()) 
       .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 
     fButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       Toast.makeText(getActivity(),"Success"+loginResult.getAccessToken(),Toast.LENGTH_LONG).show(); 

      } 

      @Override 
      public void onCancel() { 

      } 

      @Override 
      public void onError(FacebookException error) { 
       Toast.makeText(getActivity(),"Failed",Toast.LENGTH_LONG).show(); 

      } 
     }); 

     return view; 
    } 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode,resultCode, data); 
     // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
     if (requestCode == RC_SIGN_IN) { 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      handleSignInResult(result); 
     } 
    } 
    private void signIn() { 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(signInIntent, RC_SIGN_IN); 
    } 

但onActivityResult在谷歌登錄啓動時調用。

+0

請參考[這裏](http://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment) – Nikhil

回答

4

最後我得到了我的答案。 我忘了補充

fButton.setFragment(this); 

對於片段需要此setFragment(this);方法。請參閱 this

0

這是因爲你在片段整合Facebook來獲取引起onActivty導致你必須從Activty OnActivity調用它導致像

check this image for better understanding

您在活動OnACtivity結果此代碼之前超打電話給你片段將得到你的結果片段onActivity結果希望這可以幫助

+0

你說的YourFragment呢?請您及時顯示出一些例子。 –

+0

Yourfragment意思是用於集成的片段 –

+0

我的意思是Yourfragment = myFragment.class;是這樣嗎? –