3

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.

3 Answers3

14

I am not sure but this line could be the reason -

LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("public_profile", "user_friends"));

change to:

LoginManager.getInstance().logInWithReadPermissions(this,

Arrays.asList("public_profile", "user_friends"));

LoginManager has 2 methods - logInWithReadPermissions, one takes Fragment the other 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);
    }

In the end you can see that one calls Fragments startActivityForResult and the other activities -

public void startActivityForResult(Intent intent, int requestCode) {
            this.fragment.startActivityForResult(intent, requestCode);
        }

public void startActivityForResult(Intent intent, int requestCode) {
            this.activity.startActivityForResult(intent, requestCode);
        }
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
  • When I pass in **this** to `logInWithReadPermissions` android studio throws error saying it cannot resolve the method. –  Sep 25 '15 at 14:35
  • @kodephreak in your FragmentLoginButtonFacebook replace the regular Fragment import with import android.support.v4.app.Fragment; – Mikelis Kaneps Sep 25 '15 at 14:37
  • After this many years, I did managed to do the same mistake *(like passing activity ctx rather than fragment's)* & you saved the day! Thanks a lot. – Jeel Vankhede Jun 02 '20 at 18:58
1

This is quite old but, replace:

LoginManager.getInstance().logInWithReadPermissions (getActivity(), Arrays.asList("public_profile", "user_friends"));

With:

LoginManager.getInstance().logInWithReadPermissions (FragmentLoginButtonFacebook.this, Arrays.asList("public_profile", "user_friends"));
Elyakim Levi
  • 360
  • 4
  • 16
0

In my case, I have to override onActivityResult on Fragment class, not Activity class