Skip to content

Instantly share code, notes, and snippets.

Created January 19, 2016 18:21
Show Gist options
  • Save anonymous/88bd10559bdb99a59595 to your computer and use it in GitHub Desktop.
Save anonymous/88bd10559bdb99a59595 to your computer and use it in GitHub Desktop.
package xyz.app.sharetest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.ShareActionProvider;
public class MainActivity extends AppCompatActivity {
private ShareActionProvider mShareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent(getShareIntent());
// Return true to display menu
return true;
}
private Intent getShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
shareIntent.putExtra(Intent.EXTRA_TEXT, "text");
return shareIntent;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Integer id = item.getItemId();
Log.d("MainActivity", "id = " + id);
Log.d("MainActivity", "shareid = " + R.id.action_share);
Log.d("MainActivity", "Why won't this happen?");
if (id.equals(R.id.action_share)) {
Log.d("MainActivity", "Why won't this happen? 2");
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment