Android Fragments Android片段中的Facebook身份验证与导航组件

xuo3flqw  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(127)

我试图使用自定义界面登录Facebook它在Activity类中工作正常.同样的事情我也必须在Fragment中做.但它没有正确登录我得到这个屏幕时,我点击Facebook按钮

当我点击继续按钮时,我没有收到我打印的Log.i()消息
这是我的片段代码

@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();
    }

字符串
我在我的主机活动中添加了这段代码

@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);
}


我在我的应用程序中使用导航组件,当我在Activity中使用它时,相同的代码运行良好

wbrvyc0a

wbrvyc0a1#

当我将回调和LoginManager代码从片段移到主机活动时,我最终解决了这个问题,代码变成了这样

  • 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();
}

字符串
我把剩下的代码添加到我的主机活动中,

相关问题