Android: ShareActionProvider with no history
Asked Answered
C

4

8

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

mShareActionProvider.setShareHistoryFileName(null)

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(ActivityChooserModel.java:573)
11-15 10:06:34.848: E/AndroidRuntime(22461):    at android.widget.ActivityChooserModel.addHisoricalRecord(ActivityChooserModel.java:743)
11-15 10:06:34.848: E/AndroidRuntime(22461):    at android.widget.ActivityChooserModel.chooseActivity(ActivityChooserModel.java:491)
11-15 10:06:34.848: E/AndroidRuntime(22461):    at android.widget.ActivityChooserView$Callbacks.onItemClick(ActivityChooserView.java:547)

Here is the code that sets up the ShareActionProvider:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.article_pager_menu, menu);
    // mShareActionProvider is a field in the Activity
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share)
            .getActionProvider();
    mShareActionProvider
            .setShareHistoryFileName(null);
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    mShareActionProvider.setShareIntent(shareIntent);
    mShareActionProvider.onCreateActionView();
    return true;
}

Any ideas how I can fix this?

Confederation answered 15/11, 2012 at 10:30 Comment(0)
C
8

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) {
        mExpandActivityOverflowButton.setEnabled(true);
    } else {
        mExpandActivityOverflowButton.setEnabled(false);
    }
    mDefaultActivityButton.setVisibility(View.GONE);
    if (mDefaultActivityButton.getVisibility() == VISIBLE) {
        mActivityChooserContent.setBackgroundDrawable(mActivityChooserContentBackground);
    } else {
        mActivityChooserContent.setBackgroundDrawable(null);
    }
}

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

Confederation answered 18/12, 2012 at 18:47 Comment(4)
How did you extent the ActivityChooserModel which is a Singleton?Cartwright
works like a charm, just don't forget to change the necessary import in these filesBotsford
Excellent! I've been messing around with this for a while now, this works perfectly.Fossette
@AmerHadi An instance of ActivityChooserViewAdapter within the ActivityChooserView class.Confederation
B
0

Reading the source code on ActivityChooserModel I've found that the history file is open using Context's openFileInput. As long as that class keep working like that, you will be able to keep your history "clean" if you delete it using the usual method for those kinds of files:

getApplicationContext().deleteFile(SHARE_HISTORY_FILE_NAME);
shareActionProvider.setShareHistoryFileName(SHARE_HISTORY_FILE_NAME);

The "most used" icon will show for a while, when the selected application is opening, but as soon as the user is back at your app, it will disappear.

You could also delete the file on your onShareTargetSelected method, if needed.

Buhler answered 16/7, 2015 at 22:20 Comment(1)
weird, but not work, even what deleteFile returns true. As I've answered above, use random file name to make it workGilstrap
G
0

I tried all, I'm using old widget.ShareActionProvider (not compat 7), so null leads to crash, deleteFile surely deletes but history always still exists after app restart... so I've found only one working thing: random!

    String fname=ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME;
    try {
        fname = prefs.getString("SHARE_HISTORY_FILE_NAME", ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        getApplicationContext().deleteFile(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
        fname="SHARE_HISTORY_FILE_NAME"+Math.random()*1000;
        SharedPreferences.Editor ed = prefs.edit();
        ed.putString("SHARE_HISTORY_FILE_NAME", fname);
        ed.commit();
    } catch (Exception e) {
        Log.e(TAG,"err "+e.toString());
    }
    mSharedActionProvider.setShareHistoryFileName(fname);
Gilstrap answered 30/11, 2016 at 19:7 Comment(0)
M
-1

modified com.actionbarsherlock.widget.ShareActionProvider's onCreateActionView then call ActivityChooserModel's method: setHistoryMaxSize(0)

Maury answered 18/7, 2013 at 7:11 Comment(1)
The OP is not talking about action bar sherlock usage.Cartwright

© 2022 - 2024 — McMap. All rights reserved.