I'm using the Contextual Action Bar on my listview in multi mode.
During the onCreateActionMode of the CAB I initialize the ShareActionProvider. Here is the corresponding snippet:
private class modeCallback implements ListView.MultiChoiceModeListener {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getMenuInflater().inflate(R.menu.contextual_history_detail, menu);
// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
shareAction();
return true;
}
The shareAction method:
public void shareAction() {
sb = new StringBuilder();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(History_DetailActivity.this);
String shareMessageHeader = sharedPref.getString("pref_dialog_share_header_default_value", getString(R.string.pref_dialog_share_header_default_value));
sb.append(shareMessageHeader);
sb.append(System.getProperty("line.separator"));
for (long id : checkedIDs) {
Cursor cursor2 = dbHelper.getWork(id);
if (cursor2 != null && cursor2.getCount() > 0 && !cursor2.isClosed()) {
sb.append(cursor2.getString(cursor2.getColumnIndex(Consts.colWerkUren_Date)) + " ");
sb.append(cursor2.getString(cursor2.getColumnIndex(Consts.colWerkUren_Werkbon)) + " ");
sb.append(cursor2.getString(cursor2.getColumnIndex(Consts.colWerkUren_Type)) + " ");
sb.append(cursor2.getString(cursor2.getColumnIndex(Consts.colWerkUren_Comment)) + " ");
sb.append(System.getProperty("line.separator"));
}
cursor2.close();
}
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
mShareActionProvider.setShareIntent(shareIntent);
}
If I use it like this, the StringBuilder variable sb is updated but the ShareActionProvider Intent is not. I tried using the method defined here: https://stackoverflow.com/a/16281251/2119391 But according the documentation the onShareTargetSelected method doesn't allow any changes to the Intent:
Called when a share target has been selected. The client can decide whether to perform some action before the sharing is actually performed.
Note: Modifying the intent is not permitted and any changes to the latter will be ignored.
Note: You should not handle the intent here. This callback aims to notify the client that a sharing is being performed, so the client can update the UI if necessary.
Resulting in a not up to date StringBuilder sb when sharing because of the initialization of the share intent (and thus the intent extra's) in the onCreateActionMode method.
I temporarely solved the issue via very ugly (but working) code: Calling the initialization of the share intent in the onItemCheckedStateChanged in the modeCallback of the MultiChoiceModeListener.
Code snippet:
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
checkedIDs.add(id);
selCount++;
} else {
checkedIDs.remove(id);
selCount--;
}
// invalidate the CAB to be able to hide and show menu items
mode.invalidate();
// TODO Very dirty way of using the share action provider
// Is being called every change of a checked list view row
// Otherwise the share intent isn't up to date
shareAction();
}