-
-
Save anonymous/88bd10559bdb99a59595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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