ShareActionProvider refresh text
Asked Answered
U

3

11

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.

Uhl answered 16/10, 2012 at 21:12 Comment(0)
U
8

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) {
        }});
Uhl answered 17/10, 2012 at 20:9 Comment(0)
J
2

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.

Jacquelinjacqueline answered 5/7, 2013 at 10:25 Comment(0)
S
1

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.

Strathspey answered 16/10, 2012 at 21:20 Comment(4)
Thanks for you answer, but unfortunately that did not work :(Uhl
I tried this, only as a first test I put a Log message inside the onPrepareSubMenu. There is not logging, so it seems it is not passed this method when pressing the ShareActionProvider menu-item.Uhl
Yes I have: <menu xmlns:android="schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item_myshare_action_provider_action_bar" android:showAsAction="always" android:title="@string/action_bar_share_with" android:actionProviderClass="com.mydomain.MyActionProvider" /> </menu>Uhl
I can confirm, that using ActionBarSherlock the onPrepareSubMenu() method is never called.Aquarium

© 2022 - 2024 — McMap. All rights reserved.