简体   繁体   中英

Android Facebook Login onActivityResult

I use fragment for facebook login. 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. 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. Listen the results in the onActivityResult inside your Activity. Put the code below in the Activity which holds the fragment.

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.

You are using onActivityResult() wrong - you do not call super class implementation unless you know you need to. This got nothing with the need of calling super ie in onCreate() etc. Here's documentation you should read now:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM