title: Android setShareIntent within fragment
body: <p><strong>1. Background</strong></p>
  
  <p>I have a screen that has a </p>
  
  <pre><code>ShareActionProvider
  </code></pre>
  
  <p>and a </p>
  
  <pre><code>ViewPager
  </code></pre>
  
  <p>that uses fragments.</p>
  
  <p>What I was hoping to do was get some information from inside the currently visible fragment to create an intent, I would then be able to set the intent on the ShareActionProvider.</p>
  
  <p>This is the code I use to set the intent of the ShareActionProvider:</p>
  
  <pre><code>    MenuItem actionItem = men.getMenu().findItem(R.id.menu_item_share_action_provider_action_bar);
      ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
      actionProvider.setShareIntent(createShareIntent(mProduct.mProduct));
  </code></pre>
  
  <p>I have tried using this in several places such as within these functions of the Fragment class :</p>
  
  <pre><code>onCreateView
  onStart
  </code></pre>
  
  <p>I have also tried using it in these functions within the FragmentPagerAdapter class :</p>
  
  <pre><code>getItem
  </code></pre>
  
  <p><strong>2. The Problem</strong></p>
  
  <p>Although the intent is actually getting set within the ShareActionProvider, the information that is obtained is for the next fragment (the one not currently being shown). For example:</p>
  
  <p>If I have 4 fragments : frag1, frag2, frag3, frag4
  and I am currently viewing "frag1" the ShareActionProvider will attempt to share "frag2". This is true until it reaches "frag4" where it will share the correct value.</p>
  
  <p>My guess is that the fragment pager creates the current view and the next view (hidden), which is in turn setting the ShareActionProvider. If this is the case then where is the correct place to "setShareIntent"?</p>