2015-04-03 65 views
0

我使用Facebook ShareDialog共享链接和照片,具体取决于内容。但是我看到一个奇怪的问题。当我点击我的shareActionProvider中的Facebook图标时,它首先用空白帖子打开ShareDialog。然后,当我点击返回到我的应用程序时,它会重新打开ShareDialog,并显示我想要显示的链接/照片内容。Android Facebook共享正在打开shareDialog两次

这是我用来分享的代码。

.... 

shareActionProvider.setOnShareTargetSelectedListener(new MyActionProvider.OnShareTargetSelectedListener() { 
     @Override 
     public boolean onShareTargetSelected(MyActionProvider source, Intent intent) { 
      // Recover selected application name for custom action handling 
      final String appName = intent.getComponent().getPackageName(); 

      switch (appName) { 
       case "com.facebook.katana":     // Facebook 

         ShareLinkContent content = new ShareLinkContent.Builder() 
           .setContentUrl(Uri.parse("https://developers.facebook.com")) 
           .setContentTitle("Check it out!") 
           .build(); 

         shareDialog.show(content); 

         break; 

       .... 
      } 

有没有人见过这种行为?

谢谢!

我随信附上了我看到两个屏幕,以便我看到他们进来。

First screen

Second dialog screen

编辑

我添加更多我的代码。

我使用一个自定义ShareActionProvider分享到FB(这是从Android源代码的精确复制,除了我重写setOnShareTargetSelectedListener

在我的活动onCreate

FacebookSdk.sdkInitialize(getApplicationContext()); 
callbackManager = CallbackManager.Factory.create(); 
shareDialog = new ShareDialog(this); 
shareDialog.registerCallback(
      callbackManager, 
      shareCallback); 

当我设置我的ShareActionProvider

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.setType("text/plain"); 

if (shareActionProvider != null) { 
    shareActionProvider.setShareIntent(shareIntent); 
    //... this is before the setOnShareTargetSelected call .... 
+0

comment out shareDialog.show(content);它仍然显示空白对话框? – charliebeckwith 2015-04-03 23:34:13

+0

@Solarnum现在它只显示空白对话框。之后没有显示正确的一个。 – 2015-04-03 23:36:36

+0

hooray!那是你的问题。 – charliebeckwith 2015-04-03 23:38:24

回答

0

好吧,我找到了解决方案。

因为我在调用Facebook ShareDialog之前设置了shareIntent,所以它被调用了两次。

在我的自定义ShareActionProvider中,我已经覆盖了ShareActivityChooserModelPolicy类。它是这样的:

/** 
* Policy that delegates to the {@link OnShareTargetSelectedListener}, if such. 
*/ 
private class ShareActivityChooserModelPolicy 
     implements MyActivityChooserModel.OnChooseActivityListener { 
    @Override 
    public boolean onChooseActivity(MyActivityChooserModel host, Intent intent) { 
     if (mOnShareTargetSelectedListener != null) { 
      mOnShareTargetSelectedListener.onShareTargetSelected(MyActionProvider.this, intent); 
     } 
     return false; 
    } 
} 

this后:此类电话监听器,但随后返回false不管。从此方法返回true将允许我们处理意图,而不会调用默认行为。

所以我所做的就是变化

mOnShareTargetSelectedListener.onShareTargetSelected(MyActionProvider.this, intent); 

return mOnShareTargetSelectedListener.onShareTargetSelected(MyActionProvider.this, intent); 

,并呼吁shareDialog.show(content)

这解决了我的问题后添加return true

希望它可以帮助别人!

+0

你能解释一下吗?你可以显示'MyActivityChooserModel'的实现吗? – 2016-06-18 10:45:19

+0

@ShajeelAfzal不幸的是,我不能访问我实现的代码(就像以前的工作一样)。 – 2016-06-20 14:39:15

+0

虽然我相信我只是复制了ActivityChooserModel类的默认实现,并将一些返回值从false更改为true。你必须检查一下自己 – 2016-06-20 14:45:35