How do you turn off share history when using ShareActionProvider? - android

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?

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

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.

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

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

Point of clarification: It's not the "last used", it's "most often used", across a sliding window period of time.
If you prefer not to use history, then before creating your views, call
yourShareActionProvider.setShareHistoryFileName(null);
Description of this method, from the official docs (emphasis mine):
Sets the file name of a file for persisting the share history which history will be used for ordering share targets. This file will be used for all view created by onCreateActionView(). Defaults to DEFAULT_SHARE_HISTORY_FILE_NAME. Set to null if share history should not be persisted between sessions.
EDIT: I should clarify — The "most often used" item won't show up if there's no history, so this is currently the only way of removing that button.

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

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

getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);
// Set file with share history to the provider and set the share intent.
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
***actionProvider.setShareHistoryFileName(null);
OnShareTargetSelectedListener listener = new OnShareTargetSelectedListener() {
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
startActivity(intent);
return true;
}
};
actionProvider.setOnShareTargetSelectedListener(listener);***

Related

Android: Data passed with intent disappears when coming back from deeper hierarchy

I have an OverviewActivity that contains a listview. When an item is selected, an intent is created to move to the DetailActivity and I pass an int with it.
This int is assigned to a private variable and is used to query the database.
DetailActivity code:
private int mIssueId;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_issue_detail);
mIssueId = getIntent().getIntExtra(IssueOverviewFragment.INTENT_ISSUE_ID, -1);
...
}
In the DetailActivity I can go to a GraphActivity. But when I press the upButton in the GraphActivity, the application crashes because the variable became -1 in the DetailActivity (and the database can thus not be queried properly).
The hierarchy is:
OverviewActivity -> DetailActivity -> GraphActivity
GraphActivity code:
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_graph );
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_logout: {
Utility.redirectToLogin(this);
break;
}
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
How do I retain the values of my mIssueId attribute in the DetailActivity?
The Intent intends to pass information between activities. When Activity1 pass control to Activity2 mostly related to the behavior of the activity.
If you have information you need to share across a complex hierarchy or to be available for your entire app, you can choice between Shared Preferences if you need persistence or use a Singleton class if the data only will be needed while the app is running.
This is a sample for a Singleton class keeping information to available to the entire app:
public class AppData {
private static AppData ourInstance = new AppData ();
public int score;
public static AppData getInstance () {
return ourInstance;
}
}
And how to access it:
AppData.getInstance().score = 100;
Hope it helps.
It looks like you are getting as a default value because there was an issue getting the intent in DetailActivity. You could try breaking up your request a little with
mIssueId = getIntent().getExtras().getInt();
But I think the issue is probably with how you are putting the int into the intent.
It should look something like
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(IssueOverviewFragment.INTENT_ISSUE_ID, mIssueId);
startActivity(intent);

How to add two onCreateOptionsMenu(Menu menu) in android studion

I am creating an android app in which I have all ready added a share option to share content of the app, but I want to add another share option which will be able to share app download link (share this app), both the options use on create option menu, can anyone please tell me if it is possible to add two on create option or is there other way to add second share action.
Following is the code I have used for "share this app" action.
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
return intent;
}
menu_main
<item
android:title="Share"
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:icon="#drawable/share"
/>
<item
android:id="#+id/share_this_app"
android:title="share this app"
android:showAsAction="never"
android:actionProviderClass="android.widget.ShareActionProvider"/>
Have you tried something like this? Let me know if it works.
private ShareActionProvider mShareActionProvider;
private ShareActionProvider mShareActionProvider2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
mShareActionProvider2 = (ShareActionProvider) menu.findItem(R.id.share_this_app).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
mShareActionProvider2.setShareIntent(getDefaultShareIntent2());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
return intent;
}
private Intent getDefaultShareIntent2(){
/*Your coude here*/
}

SetShareIntent when a new fragment is displayed

My question is similar to Android setShareIntent within fragment, but I read the answers there and couldn't figure out how to apply them to my situation.
Quick summary of my question
I'd like to setShareIntent each time the fragment changes, i.e. whenever a new fragment is shown to the user. How can I do that? Where should the setShareIntent call go?
Longer version with code snippets
Here's a skeleton of my code:
import android.support.v7.widget.ShareActionProvider;
public class SolvePuzzle extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
static AppSectionsPagerAdapter mAppSectionsPagerAdapter;
static ViewPager mViewPager;
...
public void onCreate(Bundle savedInstanceState) {
...
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
}
}
Then comes
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.solve_puzzle, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
if (mShareActionProvider == null) {
// Following https://stackoverflow.com/questions/19358510/why-menuitemcompat-getactionprovider-returns-null
mShareActionProvider = new ShareActionProvider(this);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Some text"); // For debugging
shareIntent.setType("text/plain");
setShareIntent(shareIntent);
Log.d(DEBUG_TAG, "Called new mShareActionProvider(this) and set share intent");
}
return true;
}
private void setShareIntent(Intent shareIntent) {
// Want to call this whenever new puzzle fragment is displayed
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
} else {
Log.d(DEBUG_TAG, "Could not set share intent: mShareActionProvider == null");
}
}
I then have a public class AppSectionsPagerAdapter extends FragmentPagerAdapter and a public class SolvePuzzleFragment extends Fragment implements OnClickListener.
Currently I call setShareIntent in the onCreateView of SolvePuzzleFragment.
That doesn't work: when I look at my logcat, I see that the setShareIntent call coming from the fragments (first two lines) happens before the onCreateOptionsMenu call from the SolvePuzzle class (last line):
12-12 12:39:26.505: D/Debug(3864): Could not set share intent:
mShareActionProvider == null
12-12 12:39:26.515: D/Debug(3864): Could not set share intent:
mShareActionProvider == null
12-12 12:39:26.565: D/Debug(3864): Called new
mShareActionProvider(this) and set share intent
...and it looks like there are two calls from the fragments. Is that because both the current fragment and the next one in line are created (have their onCreateView called), even though only the first one is being displayed?
It looks like calling setShareIntent in the fragment's onCreateView is a mistake. What I want to do is call setShareIntent when a new fragment is displayed to the user. How do I do that?
Edit: Additional information:
The share button currently works, but it sends the "Some text" message that I set for debugging purposes in onCreateOptionsMenu. I'd like that intent to be overwritten by a fragment-related intent as soon as the first fragment is displayed to the user (and overwritten each time a new fragment is displayed).
How can I do that? Where should the setShareIntent call go?
Register an OnPageChangeListener with your ViewPager via setOnPageChangeListener() and put your setShareIntent() call in onPageSelected() of the listener.

Android: Title of the action bar disappears

so, in my main activity, on the onCreate() method, I check if it is the app first run with shared preferences... If it is the first run of the app, the user is redirected to a welcome activity, and then, when I press the back button and return to the main activity, the title in the action doesn't show up.. I have tested with api 9 and 17, and this only happens with api 9, so I'm guessing the error must be something about using the support library for the action bar .. Can someone help me ?
Main:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
SessionManager session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// session
session = new SessionManager(getApplicationContext());
// check first time app run
session.checkFirstRun();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SessionManager First Run check method
// check first run
public void checkFirstRun() {
if(getFirstRunStatus() == true) {
// set first run key as false
editor.putBoolean("FIRST_RUN", false);
editor.commit();
// first time running the app, redirect user to welcome activity
Intent i = new Intent(_context, WelcomeActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
public boolean getFirstRunStatus() {
return pref.getBoolean(FIRST_RUN, true);
}
Add the title in a couple ways:
XML:
<activity
android:name=".....WelcomeActivity"
android:icon="#drawable/logo"
android:label="#string/app_name"
</activity>
On the Fly:
.setTitle("TITLE");
.setIcon(R.drawable.logo);
You can pass the title in an intent to if you want it to be dynamic... not sure if that is what you want:
.setTitle(extras.getString("title"));
Hope that helps.
try this in your onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu_actions, menu);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("title");
actionBar .setDisplayShowTitleEnabled(true);
// OR:
// getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
return true;
}

Problem passing a Bundle with onSearchRequested

I'm actually trying to use the built-in search interface of Android, but I have some issues when I try to pass data with the search query.
Here is a brief explanation : I have an object in a first Activity (FirstActivity) called "Category" which implements Serializable (I already pass it successfuly between Activities) and I want to perform a search related to that category, and display the results in a second Activity (SecondActivity).
So, in FirstActivity I override the onSearchRequest method :
#Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putSerializable("category", _currentCategory);
Log.d(Utils.LOG_TAG, "Bundle : "+appData.keySet());
startSearch(null, false, appData, false);
return true;
}
And in SecondActivity, I try to get this Bundle :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
handleIntent(getIntent());
}
private void handleIntent(Intent intent){
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if(appData == null) Log.d(Utils.LOG_TAG, "appData == null");
Log.d(Utils.LOG_TAG, "Extras : "+intent.getExtras().keySet());
}
Problem is that appData seems to be equals to null everytime. Here is the logcat output :
Bundle : [category]
appData == null
Extras : [query, user_query]
I tried to add some other objects into the Bundle (Booleans, etc...) but it doesn't change anything at all and I keep having a null appData.
I had problems figuring this out as well, and the examples I found didn't really help. A lot of them suggested overriding onSearchRequested(), but that actually doesn't work for SearchWidget. I ended up using the following (from danada) as a solution, since it seemed much simpler for me than setting up the OnQueryTextListener. I just overrode startActivity (in the first, search Activity) like so:
#Override
public void startActivity(Intent intent) {
//check if search intent
if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
intent.putExtra("KEY", "VALUE");
}
super.startActivity(intent);
}
Then in the second, searchable Activity, I pulled out the info like so (called from onCreate() or from overriding onNewIntent() (if using singleTop)):
private void handleIntent(Intent intent){
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
mSearchedQuery = intent.getStringExtra(SearchManager.QUERY);
mExtraData = intent.getStringExtra("KEY");
}
Simple, and worked like a charm! Check the link to the article above if you would like a little more explanation about it.
If you're using SearchView, it will not send your appData. Instead, consider using OnQueryTextListener. For example:
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your-menu-id, menu);
/*
* Get the SearchView and set the searchable configuration.
*/
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.your-search-menuitem-id)
.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
/*
* Set query text listener here.
*/
searchView.setOnQueryTextListener(mSearchViewOnQueryTextListener);
return true;
}// onCreateOptionsMenu()
...
private final SearchView.OnQueryTextListener mSearchViewOnQueryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
/*
* You don't need to deal with "appData", because you already
* have the search query here.
*/
// Tell the SearchView that we handled the query.
return true;
}// onQueryTextSubmit()
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}// onQueryTextChange()
};// mSearchViewOnQueryTextListener
Note: You still need to keep the old way (using appData inside onSearchRequested()). In your onCreate(), if the extra for SearchManager.APP_DATA is null, that means you already handled the search query in the listener.
Conclusion:
If the SearchView is inactive, and you invoke it via onSearchRequested(), this will happen: onSearchRequested() >> onCreate() (ACTION_SEARCH contains SearchManager.APP_DATA).
If the SearchView is active, the user types and submits search, this will happen: SearchView.OnQueryTextListener.onQueryTextSubmit() >> onCreate() (ACTION_SEARCH without SearchManager.APP_DATA).
While putting data and retrieving it you are using two different keys. while putting you are using "category" and while retrieving you are using SearchManager.APP_DATA instead of using "category"
Try with
Bundle appData = intent.getBundleExtra("category");
Thanks
Deepak
In your example, you are asking for the keyset on the original intent object, and not the Bundle containing your appData. Here is an example that should work:
private void handleIntent(Intent intent){
final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
for (String key : appData.keySet()) {
Log.d(TAG, "key="+appData.getString(key));
}
}

Categories

Resources