Android setShareIntent within fragment

北战南征 提交于 2019-12-02 05:56:03

I stumble on this question while dealing with similar problem. 2 things about accepted solution:

  1. Don't save reference to the fragment anywhere, especially in some list in the adapter
  2. 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:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem menuShare = menu.findItem(R.id.menuShare);
    ShareActionProvider shareAction = (ShareActionProvider) 
         menuShare.getActionProvider();
    shareAction.setShareIntent(createShareIntent());
}

protected Intent createShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    Job job = getArguments().getSerializable(JOB);
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, job.title);
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, job.toText());
    return shareIntent;
}

I managed to figure out how to do this myself. What I ended up doing is overriding the

OnPageChangeListener

in the fragmentactivity

private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        TestFragment frag = (TestFragment) adapter.mFragments.get(pager.getCurrentItem());

        frag.setShareActionIntent();

    }

};

http://developer.android.com/reference/android/widget/ShareActionProvider.html

the example from android sdk doc is:

  // In Activity#onCreateOptionsMenu
  public boolean onCreateOptionsMenu(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.
  public void doShare(Intent shareIntent) {
      // When you want to share set the share intent.
      mShareActionProvider.setShareIntent(shareIntent);
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!