我正在尝试使用Facebook和Twitter登录创建应用。我希望每个登录按钮都在其自己的片段中,完全独立于使用这些按钮的任何活动。所以我试图将自定义按钮的onClick
处理程序放在片段本身中。
当我点击它时,它会启动facebook登录活动,但是当我在facebook登录活动中点击OK时,facebook活动会关闭,但onActivityResult
永远不会被调用。这是我的代码片段。请告诉我哪里出错了
public class FragmentLoginButtonFacebook extends Fragment {
private static final String LOG_TAG = FragmentLoginButtonFacebook.class.getSimpleName();;
CallbackManager callbackManager;
public FragmentLoginButtonFacebook() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getActivity(), "success", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(getActivity(), "cancel", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_login_facebook, container, false);
Button button = (Button) view.findViewById(R.id.facebookLoginButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("public_profile", "user_friends"));
}
});
return view;
}
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
我对android编程很新。详细解释将有很大帮助。感谢。
答案 0 :(得分:10)
我不确定,但这条线可能是原因 -
LoginManager.getInstance().logInWithReadPermissions(
getActivity(),Arrays.asList("public_profile", "user_friends"));
更改为:
LoginManager.getInstance().logInWithReadPermissions(
此,
Arrays.asList("public_profile", "user_friends"));
LoginManager有2个方法--logInWithReadPermissions,一个需要Fragment另一个Activity:
public void logInWithReadPermissions(Fragment fragment, Collection<String> permissions) {
this.validateReadPermissions(permissions);
Request loginRequest = this.createLoginRequest(permissions);
this.startLogin(new LoginManager.FragmentStartActivityDelegate(fragment), loginRequest);
}
public void logInWithReadPermissions(Activity activity, Collection<String> permissions) {
this.validateReadPermissions(permissions);
Request loginRequest = this.createLoginRequest(permissions);
this.startLogin(new LoginManager.ActivityStartActivityDelegate(activity), loginRequest);
}
最后你可以看到一个调用Fragments startActivityForResult和其他活动 -
public void startActivityForResult(Intent intent, int requestCode) {
this.fragment.startActivityForResult(intent, requestCode);
}
public void startActivityForResult(Intent intent, int requestCode) {
this.activity.startActivityForResult(intent, requestCode);
}
答案 1 :(得分:1)
这已经很老了,但是,替换:
LoginManager.getInstance().logInWithReadPermissions (getActivity(), Arrays.asList("public_profile", "user_friends"));
使用:
LoginManager.getInstance().logInWithReadPermissions (FragmentLoginButtonFacebook.this, Arrays.asList("public_profile", "user_friends"));
答案 2 :(得分:0)
就我而言,我必须在Fragment类而不是Activity类上覆盖onActivityResult