Android Setshareintent Within Fragment
1. Background I have a screen that has a ShareActionProvider and a ViewPager that uses fragments. What I was hoping to do was get some information from inside the currently vis
Solution 1:
I stumble on this question while dealing with similar problem. 2 things about accepted solution:
- Don't save reference to the fragment anywhere, especially in some list in the adapter
- Don't create fields in the fragment, always store data you need in the Bundle so you can get it by using Fragment#getArguments
Now - with this in mind here's a simple solution: put your ShareActionProvider initialization code to onPrepareOptionsMenu
call. Something like this:
@OverridepublicvoidonPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItemmenuShare= menu.findItem(R.id.menuShare);
ShareActionProvidershareAction= (ShareActionProvider)
menuShare.getActionProvider();
shareAction.setShareIntent(createShareIntent());
}
protected Intent createShareIntent() {
IntentshareIntent=newIntent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
Jobjob= getArguments().getSerializable(JOB);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, job.title);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, job.toText());
return shareIntent;
}
Solution 2:
I managed to figure out how to do this myself. What I ended up doing is overriding the
OnPageChangeListener
in the fragmentactivity
privatefinalOnPageChangeListenermOnPageChangeListener=newOnPageChangeListener() {
@OverridepublicvoidonPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonPageSelected(int arg0) {
// TODO Auto-generated method stubTestFragmentfrag= (TestFragment) adapter.mFragments.get(pager.getCurrentItem());
frag.setShareActionIntent();
}
};
Solution 3:
http://developer.android.com/reference/android/widget/ShareActionProvider.html
the example from android sdk doc is:
// In Activity#onCreateOptionsMenupublicbooleanonCreateOptionsMenu(Menu menu) {
// Get the menu item.MenuItem menuItem = menu.findItem(R.id.my_menu_item);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
// Set history different from the default before getting the action// view since a call to MenuItem.getActionView() calls// onCreateActionView() which uses the backing file name. Omit this// line if using the default share history file is desired.
mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");
. . .
}
// Somewhere in the application.publicvoiddoShare(Intent shareIntent) {
// When you want to share set the share intent.
mShareActionProvider.setShareIntent(shareIntent);
}
Post a Comment for "Android Setshareintent Within Fragment"