I have an EditText which is initially filled with text from the local db. When the user leaves the screen (onPause), the updated text is stored in the local db. I also have a ShareActionProvider (using ActionBarSherlock).
When the user uses the ShareActionProvider, the old text is send to the target application. How can I refresh the text send through the actionprovider when the user presses the menu-item?
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.share_action_provider, menu);
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
mActionProvider = (ShareActionProvider) actionItem.getActionProvider();
mActionProvider.setShareIntent(createShareIntent());
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString());
return shareIntent;
}
It seems onOptionsItemSelected() is not called when the user presses the menu-item. So I tried the following onPause(), without luck:
@Override
public void onPause() {
super.onPause();
mActionProvider.setShareIntent(createShareIntent());
// save to db:
getActivity().saveText(editText.getText().toString());
}
BTW: This code is all in a Fragment.
Perhaps a bit overkill, but I got it working by setting the share Intent every time the EditText field is changed. I added a TextWatcher listener:
editText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
if(mActionProvider!=null) {
mActionProvider.setShareIntent(createShareIntent());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}});
I stumbled across the same issue and I solved by having my share intent as a member variable in my activity. I set it as the share intent in the ShareActionProvider
in onCreateOptionsMenu
and then I can update the intent from anywhere in the activity. To avoid having to recreate your intent and having double entries in it, you can put your updates in a Bundle
and replace the extras in the intent with Intent.replaceExtras(Bundle extras)
. This worked for me. Good luck.
To update the the shareIntent i guess you will have to override onPrepareOptionsMenu too
So in OnPrepareOptionsMenu. OnPrepareOptionsMenu is called everytime just before menu is shown. call setshareintent again
mActionProvider.setShareIntent(createShareIntent());
Edit: Try this
private class MyActionProvider extends ShareActionProvider{
public MyActionProvider(Context context) {
super(context);
}
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
setShareIntent(createshareintent());
super.onPrepareSubMenu(subMenu);
}
}
And use this class in R.id.menu_item_share_action_provider_action_bar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With