简体   繁体   English

Facebook callbackManager 方法 onActivityResult 未在 Fragment onActivityResult 方法中调用

[英]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.我已经实现了 Facebook 集成,它在初始化的部分工作正常,即使 Facebook APPID 也适用于应用程序。

So let me explain in more detail : I have added and initialize Facebook in onCreate() .所以让我更详细地解释一下:我在onCreate()添加并初始化了 Facebook。

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

Also override a method in fragment as well in Fragment Activity.也在 Fragment Activity 中覆盖 fragment 中的方法。

@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.此方法在ActivityFragment类中都是相同的。

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.但问题是当我请求 facebook AppInviteContent显示一个AppInviteDialog 时,它在 Activity 中被完美调用 onActivityResult 方法,但在 Fragment 方法中没有被调用。

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根据 facebook文档,在片段中,您应该调用此方法

> Java 

loginButton.setFragment(this);
> Kotlin

loginButton.fragment = this

Could'nt find solution in stackoverflow so, i have done this my own在 stackoverflow 中找不到解决方案,所以我自己做了

-> add callbackmanager in a java class globalvaluesclass,which can be accessible by both activity and fragment -> 在java类globalvaluesclass中添加callbackmanager,activity和fragment都可以访问

->in onCreate of your parent activity add this Globalvalues.callbackManager = CallbackManager.Factory.create(); -> 在你的父活动的 onCreate 添加这个 Globalvalues.callbackManager = CallbackManager.Factory.create();

->in your fragment start your facebook login class with Globalvalues.callbackManager -> 在您的片段中使用 Globalvalues.callbackManager 启动您的 facebook 登录类

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

#all that fb stuff

} }

-> override onactivity result in your parent activity. -> 在您的父活动中覆盖 onactivity 结果。

@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.所以我明确要求使用 Activity 实例作为登录结果的接收器。 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:但我仍然面临这个问题,没有为我调用callbackManager ,所以我添加了onActivityResult()如下:

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:上面代码的问题是onActivityResult现在已被弃用,因此为了删除已弃用的代码,我修改了以下代码:

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()删除弃用代码所需的更改是在logInWithReadPermissions()时传递callbackManager引用

Used the facebook latest library version:使用了 facebook 最新的库版本:

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

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

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