Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShareActionProvider refresh text

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.

like image 210
Arno van der Ende Avatar asked Oct 16 '12 21:10

Arno van der Ende


3 Answers

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) {
        }});
like image 171
Arno van der Ende Avatar answered Nov 09 '22 23:11

Arno van der Ende


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.

like image 2
Carl B Avatar answered Nov 09 '22 23:11

Carl B


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.

like image 1
nandeesh Avatar answered Nov 09 '22 23:11

nandeesh