2015-04-03 69 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