簡體   English   中英

Android Facebook登錄onActivityResult

[英]Android Facebook Login onActivityResult

我使用片段登錄Facebook。 通常情況下一切正常。 但是在第一次,當用戶使用Facebook登錄時,他/他重定向到Facebook頁面並授予程序權限,但是(盡管我使用CallbackManager.onActivityResult),程序無法處理回調並保持后台。 我該如何處理回調? 相對代碼段:

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

在片段內部,您無法直接收聽onActivityResult。 在Activity內的onActivityResult中監聽結果。 將下面的代碼放在保存該片段的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);
      }
}

然后片段中的OnActivityResult()將起作用。

您使用的onActivityResult()錯誤-除非您知道需要,否則不要調用超類實現。 這並不需要在onCreate()等中調用super。這里是您現在應該閱讀的文檔:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM