简体   繁体   English

Android Facebook登录onActivityResult

[英]Android Facebook Login onActivityResult

I use fragment for facebook login. 我使用片段登录Facebook。 Normally everything goes fine. 通常情况下一切正常。 But in first time , when user login with facebook s/he redirected to facebook page and give permission to program but then (despite i use CallbackManager.onActivityResult) , program cannot handle the callback and stay background. 但是在第一次,当用户使用Facebook登录时,他/他重定向到Facebook页面并授予程序权限,但是(尽管我使用CallbackManager.onActivityResult),程序无法处理回调并保持后台。 How can i handle the callback ?? 我该如何处理回调? Relative code snippet: 相对代码段:

public void onCreate(Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        super.onCreate(savedInstanceState);
        callbackManager = CallbackManager.Factory.create();
    }

    public void onResume() {
        Log.i("tago" , "onResume");
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        if (profile != null) {
            tumisim = profile.getName();
            firstname = profile.getFirstName();
            lastname = profile.getLastName();
            sharedtumisimkaydet(tumisim);
            sharedfirstnamekaydet(firstname);
            sharedlastnamekaydet(lastname);
        }
    }

    public void onStop() {
        super.onStop();
        getActivity().finish();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.facebookfragment, container, false);
        int[] taniticiresimler = {R.mipmap.aciklama,R.mipmap.dene_uc,R.mipmap.yenigus,R.mipmap.galp};
        CustomPagerAdapter pagerAdapter = new CustomPagerAdapter(getActivity(),taniticiresimler);
        FadingIndicator indicator = (FadingIndicator)view.findViewById(R.id.circleIndicator);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
        viewPager.setAdapter(pagerAdapter);
        indicator.setViewPager(viewPager);
        indicator.setFillColor(Color.BLUE);
        indicator.setStrokeColor(Color.BLACK);
        indicator.setRadius(15f);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("user_friends", "public_profile", "email"));
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                profile = Profile.getCurrentProfile();
                if (profile.getId() != null) {
                    facebookID = profile.getId();
                    String a = sharedFacebookIDAl();
                    if (a.equals("defaultfacebookID")) {
                        sharedilkgiriskaydet(true);
                    }else if(!a.equals(facebookID)){
                        sharedilkgiriskaydet(true);
                    }else{
                        sharedilkgiriskaydet(false);
                    }
                    sharedfacebookIDkaydet(facebookID);
                    KullaniciProfilCek kPC = new KullaniciProfilCek();
                    kPC.execute(profile.getId());
                }
                GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {
                                    email = object.getString("email");
                                    sharedemailkaydet(email);
                                    cinsiyet = object.getString("gender");
                                    sharedcinsiyetkaydet(cinsiyet);
                                    coverphotourl = object.getJSONObject("cover").getString("source");
                                    sharedcoverphotourlkaydet(coverphotourl);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "email,gender,cover");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                Toast.makeText(getActivity(), "Facebook Login iptal edildi", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(getActivity(), "Facebook Login hata oluşturdu", Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
        Log.i("tago" , "geri geldi");
    }

Inside the fragment you cannot listen onActivityResult directly. 在片段内部,您无法直接收听onActivityResult。 Listen the results in the onActivityResult inside your Activity. 在Activity内的onActivityResult中监听结果。 Put the code below in the Activity which holds the fragment. 将下面的代码放在保存该片段的Activity中。

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  super.onActivityResult(requestCode, resultCode, intent);
  Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
  if (fragment != null) {


        fragment.onActivityResult(requestCode, resultCode, intent);
      }
}

and then the OnActivityResult() inside the fragment will work. 然后片段中的OnActivityResult()将起作用。

You are using onActivityResult() wrong - you do not call super class implementation unless you know you need to. 您使用的onActivityResult()错误-除非您知道需要,否则不要调用超类实现。 This got nothing with the need of calling super ie in onCreate() etc. Here's documentation you should read now: 这并不需要在onCreate()等中调用super。这里是您现在应该阅读的文档:

https://developer.android.com/training/basics/intents/result.html https://developer.android.com/training/basics/intents/result.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中使用Facebook登录时未调用onActivityResult - onActivityResult is not calling while using facebook login in android 在Android上登录ActivityResult后,Facebook SDK应用程序崩溃 - Facebook SDK app crash after login onActivityResult Android 在Android中登录Facebook后,会话在OnActivityResult方法中为空 - Session is becoming null in OnActivityResult Method after login to Facebook in Android Facebook 和 Google+ 登录 Android 的 onActivityResult 代码 - onActivityResult code for both Facebook and Google+ Login in Android Android Facebook登录,onActivityResult中callbackManager尚未初始化 - Android Facebook login, callbackManager has not been initialized in onActivityResult android facebook授权:onActivityResult未调用 - android facebook authorization: onActivityResult not called onActivityResult处理Facebook和Twitter登录 - onActivityResult to handle both facebook and twitter login 使用Facebook SDK登录导致onActivityResult错误 - Login using facebook sdk gives error on onActivityResult Android Studio Facebook:普通课程中的onActivityResult - Android Studio Facebook: onActivityResult in normal class Android Facebook LoginButton onActivityResult空指针异常 - Android Facebook LoginButton onActivityResult null pointer exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM