steemcb steemcb - 8 months ago 65
Android Question

Android: ShareActionProvider with no history

According to the Android documentation if I don't want my ShareActionProvider to persist the share history I should call


However when I do this I get the following crash on selecting a share option:

11-15 10:06:34.848: E/AndroidRuntime(22461): java.lang.IllegalStateException: No preceding call to #readHistoricalData
11-15 10:06:34.848: E/AndroidRuntime(22461): at android.widget.ActivityChooserModel.persistHistoricalDataIfNeeded(
11-15 10:06:34.848: E/AndroidRuntime(22461): at android.widget.ActivityChooserModel.addHisoricalRecord(
11-15 10:06:34.848: E/AndroidRuntime(22461): at android.widget.ActivityChooserModel.chooseActivity(
11-15 10:06:34.848: E/AndroidRuntime(22461): at android.widget.ActivityChooserView$Callbacks.onItemClick(

Here is the code that sets up the ShareActionProvider:

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
// mShareActionProvider is a field in the Activity
mShareActionProvider = (ShareActionProvider) menu.findItem(
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
return true;

Any ideas how I can fix this?

Answer Source

So in the end I had to write my own ShareActionProvider by copying the one found in Android source. I also had to copy over the ActivityChooserView and the ActivityChooserModel from source. The actual modification needed to hide the default activity in the action bar is in the updateAppearance() method in the ActivityChooserView. This is how it should look:

private void updateAppearance() {
    // Expand overflow button.
    if (mAdapter.getCount() > 0) {
    } else {
    if (mDefaultActivityButton.getVisibility() == VISIBLE) {
    } else {

I couldn't figure out why setShareHistoryFileName(null) was causing the problem I originally described though. Thanks for the attempted answer Seven.