Your code above does not work because onCreateOptionsMenu is called only once when the options menu is displayed.
Fixing this is pretty simple. We build our intent when onOptionsItemSelected is onOptionsItemSelected . This is when any bloated menu resource is selected. If the selected item is a shared resource, shareURL is shareURL , which now builds and runs Intent.
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public final boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_item_share: shareURL(); } return super.onOptionsItemSelected(item); } private void shareURL() { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, web.getUrl()); startActivity(Intent.createChooser(shareIntent, "Share This!")); }
I have not tested the sample code above. Neither on the device itself, nor on the Java compiler. However, this should help you solve your problem.
source share