ShareActionProvider not clickable and not rendering properly on first render

前端 未结 4 757
無奈伤痛
無奈伤痛 2021-02-08 10:10

I have a ShareActionProvider together with some other options in my ActionBar. It seems however that the ShareActionProvider has problems rendering properly when first rendered

相关标签:
4条回答
  • 2021-02-08 10:27

    So it seems that calling "getActivity().invalidateOptionsMenu()" in onCreateView in the Fragment makes the menu re-render as it should. It should render properly in the first run though, invalidating the menu without making changes to it doesn't feel like a proper solution.

    0 讨论(0)
  • 2021-02-08 10:28

    That's because you have to add an intent to the ShareActionPRovider right after you inflate the menu, on onCreateOptionsMenu.

    If you do that only in onPrepareOptionsMenu, you'll have to manually call invalidateOptionsMenu() to trigger an ActionBar update (as the chosen answer tells you to). But that's not the way to do it.

    It works fine when the configuration changes, because onPrepareOptionsMenu() is called, and then your share button will work, since it now has an Intent.

    To fix that, just do something like this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        getSupportMenuInflater().inflate(R.menu.YOUR_MENU_XML, menu);
    
        ShareActionProvider provider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();
    
        if (provider != null) {
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, YOUR_TEXT);
            shareIntent.setType("text/plain");
            provider.setShareIntent(shareIntent);
        }
    
        return true;
    }
    

    This way, the ShareActionProvider will have an Intent from the beginning, and will work as expected.

    0 讨论(0)
  • 2021-02-08 10:31

    I don't think it's a bug. It's because your title changed. It was shorter ("Details" instead of "ReceiptDetail") originally, and so the system must have thought there was more room to show more action items.

    Also the width of the ShareActionProvider is dynamic (it can be up to more than 2 times the normal width).

    To test a thing or two, I would propose you move the share action item up to the first position, remove your temporary workaround, and see if it still occurs. You could also remove the share action item and use 3 or more conventional action items as a test.

    0 讨论(0)
  • 2021-02-08 10:34

    It seams like platform bug. you can check this http://code.google.com/p/android/issues/detail?id=25467 for more information.

    0 讨论(0)
提交回复
热议问题