Android: ShareActionProvider with no history

删除回忆录丶 提交于 2019-12-10 02:56:28

问题


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?


回答1:


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.




回答2:


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.




回答3:


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);



回答4:


add the code like this:

    private void addShareSelectedListener() {
    if (null == mShareActionProvider) return;
    OnShareTargetSelectedListener listener = new OnShareTargetSelectedListener() {
        public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
            mContex.startActivity(intent);
            return true;
        }
    };

//Set to null if share history should not be persisted between sessions.
    mShareActionProvider.setShareHistoryFileName(null);
    mShareActionProvider.setOnShareTargetSelectedListener(listener);

}

More info please click here




回答5:


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



来源:https://stackoverflow.com/questions/13395601/android-shareactionprovider-with-no-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!