Facebook authentication in android fragment with navigation component

34 views Asked by At

I'm trying to login Facebook using custom interface it worked fine in Activity class. same thing i have to do in Fragment too. but it doesn't logged in correctly I get this screen when I click on facebook button1
when I click on continue button I didn't get Log.i() message which I print
this is my fragment code

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
binding = FragmentLoginBinding.inflate(inflater, container, false);
// facebook callback
        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        loginWithFacebook(loginResult);
                    }

                    @Override
                    public void onCancel() {
                        // App code
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                        Log.e("Login With facebook", "onError: ", exception);
                    }
                });
binding.btnRegisterFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginManager.getInstance().logInWithReadPermissions(requireActivity(), Arrays.asList("public_profile"));
            }
        });
return binding.getRoot();
}
@Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

private void loginWithFacebook(LoginResult loginResult) {
        // here we will get access token and save data to server
        AccessToken accessToken = loginResult.getAccessToken();
        GraphRequest request = GraphRequest.newMeRequest(
                accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        try {
                            String name = object.getString("name");
                            String id = object.getString("id");
                            // save to server
                            Log.i("Facebook auth",id);
                            Log.i("Facebook auth",name);
                        } catch (JSONException e) {
                            Log.e("Login With facebook", "onCompleted: ", e);
                            throw new RuntimeException(e);
                        }
                        // Application code
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link");
        request.setParameters(parameters);
        request.executeAsync();
    }

and I added this code in my host activity

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
   Fragment navhost = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
   navhost.onActivityResult(requestCode,resultCode,data);
   super.onActivityResult(requestCode, resultCode, data);
}

I'm using navigation component in my app, and the same code is working well when I'm using it in activity

1

There are 1 answers

0
Mohamed Hossam On

I finally resolve the problem when I remove callback and LoginManager code from fragment to host activity and the code becomes like this
Fragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    binding = FragmentLoginBinding.inflate(inflater, container, false);
    binding.btnRegisterFacebook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginManager.getInstance().logInWithReadPermissions(requireActivity(), Arrays.asList("public_profile"));
            }
        });
    return binding.getRoot();
}

and I added remaining code into my host activity