我有一個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在谷歌登錄啓動時調用。
請參考[這裏](http://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment) – Nikhil