My question is similar to Android setShareIntent within fragment, but I read the answers there and couldn't figure out how to apply them to my situation.
Quick summary of my question
I'd like to setShareIntent each time the fragment changes, i.e. whenever a new fragment is shown to the user. How can I do that? Where should the setShareIntent call go?
Longer version with code snippets
Here's a skeleton of my code:
import android.support.v7.widget.ShareActionProvider;
public class SolvePuzzle extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
static AppSectionsPagerAdapter mAppSectionsPagerAdapter;
static ViewPager mViewPager;
...
public void onCreate(Bundle savedInstanceState) {
...
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
}
}
Then comes
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.solve_puzzle, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
if (mShareActionProvider == null) {
// Following https://stackoverflow.com/questions/19358510/why-menuitemcompat-getactionprovider-returns-null
mShareActionProvider = new ShareActionProvider(this);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Some text"); // For debugging
shareIntent.setType("text/plain");
setShareIntent(shareIntent);
Log.d(DEBUG_TAG, "Called new mShareActionProvider(this) and set share intent");
}
return true;
}
private void setShareIntent(Intent shareIntent) {
// Want to call this whenever new puzzle fragment is displayed
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
} else {
Log.d(DEBUG_TAG, "Could not set share intent: mShareActionProvider == null");
}
}
I then have a public class AppSectionsPagerAdapter extends FragmentPagerAdapter
and a public class SolvePuzzleFragment extends Fragment implements OnClickListener
.
Currently I call setShareIntent in the onCreateView of SolvePuzzleFragment.
That doesn't work: when I look at my logcat, I see that the setShareIntent call coming from the fragments (first two lines) happens before the onCreateOptionsMenu call from the SolvePuzzle class (last line):
12-12 12:39:26.505: D/Debug(3864): Could not set share intent: mShareActionProvider == null
12-12 12:39:26.515: D/Debug(3864): Could not set share intent: mShareActionProvider == null
12-12 12:39:26.565: D/Debug(3864): Called new mShareActionProvider(this) and set share intent
...and it looks like there are two calls from the fragments. Is that because both the current fragment and the next one in line are created (have their onCreateView called), even though only the first one is being displayed?
It looks like calling setShareIntent in the fragment's onCreateView is a mistake. What I want to do is call setShareIntent when a new fragment is displayed to the user. How do I do that?
Edit: Additional information:
The share button currently works, but it sends the "Some text" message that I set for debugging purposes in onCreateOptionsMenu. I'd like that intent to be overwritten by a fragment-related intent as soon as the first fragment is displayed to the user (and overwritten each time a new fragment is displayed).
How can I do that? Where should the setShareIntent call go?
Register an OnPageChangeListener
with your ViewPager
via setOnPageChangeListener()
and put your setShareIntent()
call in onPageSelected()
of the listener.