Determine if ShareActionProvider is activated

I have problems with ShareActionProvider. I like to share the current webview URL (the URL can be changed dynamically via JS) via ShareActionProvider. To do this, I rewrote onOptionsItemSelected to change the Intent via setShareIntent. But when I click on ShareActionProvider and share it through a specific application, nothing happens. I debugged it and I found out that onOptionsItemSelected is not starting. Can someone help me?

public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.game, menu); MenuItem mShareActionProviderItem = (MenuItem) menu.findItem(R.id.action_share); mShareActionProvider = (ShareActionProvider) mShareActionProviderItem.getActionProvider(); // is a field Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType("text/plain"); mShareActionProvider.setShareIntent(sendIntent); //needed to be able to click on Item mShareActionProviderItem.getActionView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl(); /*ShareButton set URL*/ Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url)); mShareActionProvider.setShareIntent(sendIntent); } }); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.action_share: /*URL holen*/ String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl(); /*ShareButton mit URL bestรผcken*/ ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider(); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url)); mShareActionProvider.setShareIntent(sendIntent); break; default: return super.onOptionsItemSelected(item); } return true; 
+4
source share
1 answer

I know this a bit later, but I had the same problem, and I solved it by specifying OnShareTArgetSelectedListener on my ShareActionProvider .:

  mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() { @Override public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) { return false; } }); 

http://developer.android.com/reference/android/widget/ShareActionProvider.html#setOnShareTargetSelectedListener(android.widget.ShareActionProvider.OnShareTargetSelectedListener)

+4
source

Source: https://habr.com/ru/post/1481805/


All Articles