我有一个带有facebook登录的Android应用程序。我在片段中初始化了facebook sdk。但是在启动登录过程时从未调用过onActivityResult。
@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);
}
但是当google登录时调用了onActivityResult。
答案 0 :(得分:7)
答案 1 :(得分:0)
这是因为您在Fragment中集成facebook以获得片段onActivty结果,您必须从Activty OnActivity结果调用它
check this image for better understanding
你在活动OnACtivity结果中的这段代码在超级调用之前你会得到你的结果片段onActivity结果希望这个帮助