I am trying to create an app with both Facebook and Twitter login. I want each login button to be in its own fragment completely independent of any activity where these are used. So I am trying to put the onClick handler for my custom button in the fragment itself.
When I click on it, it starts the facebook login activity but when I click on OK in the facebook login activity, the facebook activity closes but onActivityResult is never called. Here is my code snippet. Please tell me where am I going wrong
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);
}
}
- I am not using LoginButton in my XML. I am creating my own Button.
- There is no error in my LogCat.
I am very new to android programming. A detailed explanation would be of great help. Thanks.