Android: ShareActionProvider without history

According to the Android documentation, if I do not want my ShareActionProvider to save the sharing history, I have to call

mShareActionProvider.setShareHistoryFileName(null) 

However, when I do this, I get the following crash when choosing the sharing 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 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?

+8
android android-intent android-actionbar shareactionprovider
source share
5 answers

So, in the end, I had to write my own ShareActionProvider file, copying the file found in the Android source. I also had to copy the ActivityChooserView and ActivityChooserModel objects from the source. The actual modification needed to hide the default activity in the action bar is in the updateAppearance () method in ActivityChooserView. Here's 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 could not understand why setShareHistoryFileName (null) is causing the problem that I originally described. Thanks for trying to answer Seven.

+8
source share

Reading source code in ActivityChooserModel I found that the history file is open using the openFileInput context. As long as this class works like this, you can keep your history "clean" if you delete it using the usual method for such files:

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

The β€œmost used” icon will be displayed for a while when the selected application is opened, but as soon as the user returns to your application, it will disappear.

You can also delete the file using your onShareTargetSelected method, if necessary.

0
source share

I tried everything, I use the old widget.ShareActionProvider (not compat 7), so null crashes, deleteFile certainly deletes, but the history is always saved after restarting the application ... so I 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); 
0
source share

add the code as follows:

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

Click here for more details.

-one
source share

changed com.actionbarsherlock.widget.ShareActionProvider onCreateActionView then calls the ActivityChooserModel method: setHistoryMaxSize (0)

-one
source share

All Articles