Note that there are some explanatory texts on larger screens.

plurals
  1. POShare Intent list menu available on device orientation changed (shareActionProvider)
    text
    copied!<p>After couple of hours of investigations and trying to find a solution I’ve decided to write here my problem in idea that maybe someone else face it also and found a fix for it. I have a application with a menu, one of the options being a <strong>Share option</strong>, declare like this in menu xml file:</p> <pre><code>&lt;item android:id="@+id/menu_share" android:actionProviderClass="android.widget.ShareActionProvider" android:title="Share"/&gt; </code></pre> <p>In <code>onOptionsItemSelected</code> method I create and set the share intent:</p> <pre><code>private ShareActionProvider shareActionProvider; @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_share: MenuItem actionItem = optionsMenu.findItem(R.id.menu_share); shareActionProvider = (ShareActionProvider) actionItem.getActionProvider(); shareActionProvider.setShareIntent(createShareIntent()); return true; default: return super.onOptionsItemSelected(item); } } </code></pre> <p>When pressing the Share menu option the list with apps to share is displayed. <img src="https://i.stack.imgur.com/BYUdQ.png" alt="enter image description here"> What I want is when I rotate the device that list to be visible, but instead is disappearing. To mention that I can’t use <code>android:configChanges</code> in order to prevent the activity from being recreated.</p> <p>I wanted first to see how can I open that share apps list programmatically.</p> <p>a). Calling the below method nothing visual happens; it just updates the <code>ShareActionProvider</code></p> <pre><code>public void showShareItemList() { MenuItem actionItem = optionsMenu.findItem(R.id.menu_share); shareActionProvider = (ShareActionProvider) actionItem.getActionProvider(); shareActionProvider.setShareIntent(createShareIntent()); } </code></pre> <p>b). Calling the below method opens a new activity – but not a menu options list like in the picture above.</p> <pre><code>public void showShareItemList2(){ Intent sharingIntent = new Intent(); sharingIntent.setAction(Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here"); startActivity(Intent.createChooser(sharingIntent, "Share")); } </code></pre> <p>Result: <img src="https://i.stack.imgur.com/mqpjG.png" alt="enter image description here"></p> <p>c).The below method just opens the menu option:</p> <pre><code>public void showShareItemList3() { openOptionsMenu(); // programmatically open the options menu } </code></pre> <p><strong>So, is there a way to open the share item list of applications programmatically ? Or at least is there a way to press menu key programmatically ( = programatically select a menu item)?</strong></p> <p>Another issue is <strong>how can I know if the share list is visible on orientation changed?</strong> </p> <p>For this I’m using a <code>boolean</code> variable <code>private boolean isShareOptionMenuListVisible = false;</code> which I save it on method <code>onSaveInstanceState()</code> and restored it in <code>onCreate()</code> method. I make <code>isShareOptionMenuListVisible = true</code> when the Share item is pressed, but I could not find a way to make it to false when is not visible anymore. Using the override method <code>onOptionsMenuClosed</code> is not helping me because this <em>“is called whenever the options menu is being closed”</em> (this is in documentation, even if I can say this is not real because in my app is never called), and I want to know when the Share apps options menu is being closed.</p> <p>Here is a sample documented project which demonstrates all the above issues: <a href="https://www.dropbox.com/s/d4t2di1x70ljkt3/ShareIntent.zip" rel="nofollow noreferrer">project link</a>.</p> <p>Any help is welcome! Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload