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*/
}
Related
According to the official documentation, there are two ways of providing a search interface: using either the search dialog or a SearchView widget. I'd like to pay attention to passing search context data using these two ways.
So, the documentation says:
..you can provide additional data in the intent that the system sends to
your searchable activity. You can pass the additional data in the
APP_DATA Bundle, which is included in the ACTION_SEARCH intent.
To pass this kind of data to your searchable activity, override the
onSearchRequested() method for the activity from which the user can
perform a search, create a Bundle with the additional data, and call
startSearch() to activate the search dialog. For example:
#Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putBoolean(SearchableActivity.JARGON, true);
startSearch(null, false, appData, false);
return true;
}
..Once the user submits a query, it's delivered to your searchable
activity along with the data you've added. You can extract the extra
data from the APP_DATA Bundle to refine the search. For example:
Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
}
This refers to the search dialog. And what about the search widget?
Is it possible to pass search context data using the SearchView widget only?
Hope, someone could give clear explanation and/or suggest another or similar way to accomplish the goal.
Thanks!
I've discovered the solution. Even two solutions!
They don't need to invoke onSearchRequested() thus there is no search dialog at all :)
First, I provide some common steps to create the search interface and then give the solutions of the source problem.
We add the Search View to the App Bar by creating res/menu/options_menu.xml file with the following code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_string"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Create a Searchable Configuration in res/xml/searchable.xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
Declare two activities in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchinterface">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
</manifest>
Create a Searchable Activity where we handle the ACTION_SEARCH intent and the search context data, passed from the MainActivity. We extract the extra data from the APP_DATA Bundle to refine the search:
public class SearchableActivity extends AppCompatActivity {
public static final String JARGON = "com.example.searchinterface.jargon";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// use the query to search the data somehow
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
// use the context data to refine our search
}
}
}
}
Now, we need to implement our MainActivity class. So, we inflate our menu and configure the SearchView element as well. We also need to set the SearchView.OnQueryTextListener and implement its methods, especially onQueryTextSubmit(). It is invoked when the user presses the submit button and it contains the main logic of passing the search context data to the SearchableActivity.
Finally, we reached the main answer section. As I said, there are two solutions:
1. Create an intent with Bundle extra and send it to the SearchableActivity manually;
Here is the MainActivity with all necessary contents:
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private SearchView mSearchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
mSearchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
Intent searchIntent = new Intent(this, SearchableActivity.class);
searchIntent.putExtra(SearchManager.QUERY, query);
Bundle appData = new Bundle();
appData.putBoolean(SearchableActivity.JARGON, true); // put extra data to Bundle
searchIntent.putExtra(SearchManager.APP_DATA, appData); // pass the search context data
searchIntent.setAction(Intent.ACTION_SEARCH);
startActivity(searchIntent);
return true; // we start the search activity manually
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
Thanks to https://stackoverflow.com/a/22184137/6411150.
The second solution is also put in onQueryTextSubmit() (but it's not necessary):
2. Create search context data Bundle and pass it to the setAppSearchData() method of the SearchView.
So, we don't need to create and pass the whole search intent and launch respective searchable activity, the system will take care of it.
Here is another code snippet:
/*
You may need to suppress the “restrictedApi” error that you could possibly
receive from this method "setAppSearchData(appData)”.I had to
I’m targetSdkVersion 26. I’m also using Android Studio 3
with the new gradle plugin, which might be causing this.
If you’re not running Android Studio 3 you can simply put
“//noinspection RestrictedApi"
right above the line: mSearchView.setAppSearchData(appData);
*/
#SuppressWarnings("RestrictedApi")
#Override
public boolean onQueryTextSubmit(String query) {
Bundle appData = new Bundle();
appData.putBoolean(SearchableActivity.JARGON, true); // put extra data to Bundle
mSearchView.setAppSearchData(appData); // pass the search context data
return false; // we do not need to start the search activity manually, the system does it for us
}
Thanks to https://stackoverflow.com/a/38295904/6411150.
Note: Only support library's version of SearchView (android.support.v7.widget.SearchView) contains the setAppSearchData() method, so be attentive.
If the design works for your needs, you can use the SearchView by itself, and add an OnQueryTextListener to it, and deal with it there. There is no need for anything else, no Intents, Meta-tags, nor XML files. I have done this a few times, and the docs are a bit not clear on this.
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.
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;
}
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);***
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));
}
}