简体   繁体   中英

Facebook callbackManager method onActivityResult not calling in Fragment onActivityResult method

I have implemented Facebook integration and it's working fine in part of initialization even Facebook APPID is also fine with application.

So let me explain in more detail : I have added and initialize Facebook in onCreate() .

// Facebook callback manager
callbackManager = CallbackManager.Factory.create();

Also override a method in fragment as well in Fragment Activity.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

This method is same in both Activity and Fragment class.

But the issue is when i request of facebook AppInviteContent to show a AppInviteDialog than it is perfectly called onActivityResult method in Activity but not getting called in Fragment method.

If anyone have insight than give me any suggestion. Any comment or suggestion are welcome.

According to facebook documentations , In a fragment, you should call this method

> Java 

loginButton.setFragment(this);
> Kotlin

loginButton.fragment = this

Could'nt find solution in stackoverflow so, i have done this my own

-> add callbackmanager in a java class globalvaluesclass,which can be accessible by both activity and fragment

->in onCreate of your parent activity add this Globalvalues.callbackManager = CallbackManager.Factory.create();

->in your fragment start your facebook login class with Globalvalues.callbackManager

LoginManager.getInstance().registerCallback(Globalvalues.callbackManager, new FacebookCallback() {

#all that fb stuff

}

-> override onactivity result in your parent activity.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

       fragment.customOnActivityResult(requestCode, resultCode, data);

}

and in your fragment add a function for example as shown below

      public static void customOnActivityResult(){
          super.onActivityResult(requestCode, resultCode, data);
    Globalvalues.callbackManager.onActivityResult(requestCode, resultCode,
      data);

  }

                                                       and you're Welcome.
  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
}

I had this problem when I used following code to initiate login:

LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList(EMAIL));

so I explicitly asked to use Activity instance as receiver of login results. The solution is to provide fragment instance, for which appropriate overloaded method exists:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList(EMAIL));

This assumes call is from within fragment.

I also had the same problem, initially I was not setting the fragment reference while setting the permissions, so I modified my call like below:

loginManagerFb.logInWithReadPermissions(
     this,
     listOf("public_profile", "email")
)

But still I was facing the issue, the callbackManager was not getting called for me, so I added onActivityResult() like below:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    callbackManager.onActivityResult(requestCode, resultCode, data)
}

The issue with the above code was that the onActivityResult is now deprecated, so to remove the deprecated code I modified the code as below:

val loginManagerFb = LoginManager.getInstance()
     loginManagerFb.logInWithReadPermissions(
         this,
         callbackManager,
         listOf("public_profile", "email")
    )

The change that was required to remove the deprecated code was to pass the callbackManager reference while logInWithReadPermissions()

Used the facebook latest library version:

implementation "com.facebook.android:facebook-android-sdk:12.0.0"

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