2015-10-14 73 views
1

我想實現我的Android應用程序Facebook的登錄按鈕,這是我迄今所做的:Android的登錄按鈕沒有調用onActivityResult或回調方法

public class LoginFragment extends BaseFragment { 
    private View mView; 
    private CallbackManager mCallbackManager; 
    private LoginButton mFBLoginButton; 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     FacebookSdk.sdkInitialize(getActivity()); 
     mCallbackManager = CallbackManager.Factory.create(); 
     mView = inflater.inflate(R.layout.login, container, false); 
     mFBLoginButton = (LoginButton) mView.findViewById(R.id.login_facebook_button); 
     mFBLoginButton.setReadPermissions("public_profile", "user_friends","user_birthday","user_about_me","email"); 
     mFBLoginButton.setFragment(this); 
     mFBLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      Log.d("LoginFragment", "UserId: " + loginResult.getAccessToken().getUserId()); 
     } 

     @Override 
     public void onCancel() { 
      Log.d("LoginFragment", "Operation Canceled"); 
     } 

     @Override 
     public void onError(FacebookException e) { 
      e.printStackTrace(); 
     } 
     }); 
     return mView; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     getBaseActivity().displayToastMessage("Hey"); 
     mCallbackManager.onActivityResult(requestCode, resultCode, data); 
    } 

我還添加了Facebook活動和應用程序清單中的Id。該應用程序運行良好,當我點擊登錄按鈕,它將我帶到Facebook對話框。這裏的問題是沒有任何回調方法被執行,即onSuccess(),onCancel()和onError()。 onActivityResult()也不會被執行。請告訴我,如果我錯過了什麼

回答

3

長的檢查後,我發現我設定的noHistory該活動舉辦的片段爲真。一旦認證成功導致onActivityResult()未被調用,這就阻止了再次返回到活動。

+0

這解決了我的問題。謝了哥們 – saintjab

0

我也有這個問題,但是通過在父活動OnActivityResult函數中添加方法來解決方法。

@Override 
public void onAttachFragment(Fragment fragment) { 
    super.onAttachFragment(fragment); 
    try { 
     String fragmentSimpleName = fragment.getClass().getSimpleName(); 
     if (fragmentSimpleName.equals("Fragment1")){ 
      frag = (Fragment1) fragment; 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // facebook.authorizeCallback(requestCode, resultCode, data); 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     if (frag != null){ 
      frag.myCallBack(requestCode, resultCode, data); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
相關問題