How do you turn off share history when using Share

2019-01-11 01:12发布

The new ShareActionProvider available in Android 4.0 (or in earlier versions if you're using ActionBarSherlock) has a feature where the last used item is displayed in the action bar. Is there anyway to turn this off?

8条回答
趁早两清
2楼-- · 2019-01-11 01:35

There is no API to do this. However, the class is really simple and you could very easily create your own version of ShareActionProvider that did not keep a history. You would just have to determine the sort order of the possible targets using some other means of ordering (e.g., alphabetically).

查看更多
beautiful°
3楼-- · 2019-01-11 01:40

Start the share activity by yourself:

shareActionProvider.setShareIntent(intent);
shareActionProvider.setOnShareTargetSelectedListener(this);

@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
    // start activity ourself to prevent search history
    context.startActivity(intent);
    return true;
}

Then the ShareActionProvider will not add the chosen activity to the share history.

查看更多
爷、活的狠高调
4楼-- · 2019-01-11 01:40

I created my own version of the ShareActionProvider (and supporting classes), you can copy them into your project (https://gist.github.com/saulpower/10557956). This not only adds the ability to turn off history, but also to filter the apps you would like to share with (if you know the package name).

private final String[] INTENT_FILTER = new String[] {
        "com.twitter.android",
        "com.facebook.katana"
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.journal_entry_menu, menu);

    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);

    if (shareItem instanceof SupportMenuItem) {
        mShareActionProvider = new ShareActionProvider(this);
        mShareActionProvider.setShareIntent(ShareUtils.share(mJournalEntry));
        mShareActionProvider.setIntentFilter(Arrays.asList(INTENT_FILTER));
        mShareActionProvider.setShowHistory(false);
        ((SupportMenuItem) shareItem).setSupportActionProvider(mShareActionProvider);
    }

    return super.onCreateOptionsMenu(menu);
}
查看更多
Deceive 欺骗
5楼-- · 2019-01-11 01:41

add the code like this:

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

}
查看更多
手持菜刀,她持情操
6楼-- · 2019-01-11 01:42

For me, the best solution for avoid the history icon is don't use ShareActionProvider, instead of it, create it as any other action:

 <item 
    android:id="@+id/menu_action_share"
    android:showAsAction="ifRoom" 
    android:icon="@drawable/ic_action_share"
    android:title="@string/share"/>   

at the menu/activity_actions.xml put a item with the ic_action_share icon...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
 }

Inflate the menu normally...

private void actionShare(){
     Intent i = new Intent(Intent.ACTION_SEND);
     i.setType("text/plain");
     i.putExtra(Intent.EXTRA_SUBJECT, "my string");
     i.putExtra(Intent.EXTRA_TEXT, "another string");
     startActivity(i); 
     //Or like above will always display the chooser
     //startActivity(Intent.createChooser(i, getResources().getText(R.string.share))); 
 }

Create a method with an ACTION_SEND intent

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_share:
            actionShare();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

And finally call to this method from onOptionsItemSelected

more info ->Sending Simple Data to Other Apps

查看更多
我命由我不由天
7楼-- · 2019-01-11 01:52

Although It's been 2 years ago today but I'd like to share my experience as I made a custom ShareActionProvider class and add this line chooserView.getDataModel().setHistoryMaxSize(0); inside onCreateActionView() which did all the magic for me ! .. I tested it on Lollipop device and on API 16 emulator device and it works perfectly. here is my custom class :

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.internal.widget.ActivityChooserView;
import android.support.v7.widget.ShareActionProvider;
import android.view.View;


public class MyShareActionProvider extends ShareActionProvider {
    /**
     * Creates a new instance.
     *
     * @param context Context for accessing resources.
     */
    public MyShareActionProvider(Context context) {
        super(context);
    }

    @Override
    public View onCreateActionView() {
        ActivityChooserView chooserView = (ActivityChooserView) super.onCreateActionView();
        Drawable icon;
        if (Build.VERSION.SDK_INT >= 21) {
            icon = getContext().getDrawable(R.drawable.share_icon);
        }else{
            icon = getContext().getResources().getDrawable(R.drawable.share_icon);
        }
        chooserView.setExpandActivityOverflowButtonDrawable(icon);
        chooserView.getDataModel().setHistoryMaxSize(0);
        return chooserView;
    }

}
查看更多
登录 后发表回答