Android: Return search query to current activity Android: Return search query to current activity android android

Android: Return search query to current activity


In your Application Manifest you need to define the current activity as a searchable activity.

<activity android:name="BrowseItems" android:label="@string/browseitems"            android:launchMode="singleTop">            <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>            <meta-data android:name="android.app.searchable"                android:resource="@xml/itemsearchable" /></activity>

You then use the following code, which is from http://developer.android.com/guide/topics/search/search-dialog.html#LifeCycle

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.search);    handleIntent(getIntent());}@Overrideprotected void onNewIntent(Intent intent) {    setIntent(intent);    handleIntent(intent);}private void handleIntent(Intent intent) {    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {      String query = intent.getStringExtra(SearchManager.QUERY);      // Do work using string    }}

You can then use the string to reload your activity, if its a list activity you can call your code that you use to load data and use the string in that.


Add to AndroidManifest.xml in your Searchable Activity:

android:launchMode="singleTop" 

so, your AndroidManifest.xml looks like:

<activity android:name="MyNotepad"              android:label="@string/app_name"              android:launchMode="singleTop">        <intent-filter>            <action android:name="android.intent.action.MAIN" />            <category android:name="android.intent.category.LAUNCHER" />            <action android:name="android.intent.action.SEARCH"></action>        </intent-filter>        <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>    </activity><activity android:name="Preferences" android:label="Preferences" >

The reason:

From this post:

The activity launch mode has four valid values:

"standard""singleTop" "singleTask""singleInstance"

The 'standard' is the default value. The four values fall into two groups:

'standard' and 'singleTop' can instantiate multiple activity instances and the instance will stay in the same task.For 'singleTask' or 'singleInstance', the activity class uses the singleton pattern, and that instance will be the root activity of a new task. Let's examine each value:"standard":

Multiple instances of the activity class can be instantiated and multiple instances can be added to the same task or different tasks. This is the common mode for most of the activities.

"singleTop":

The difference from 'standard' is, if an instance of activity already exists at the top of the current task and system routes intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object.


I simply use this:-

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {        @Override        public boolean onQueryTextSubmit(String s) {            //on submit            return false;        }        @Override        public boolean onQueryTextChange(String s) {            //get all text changes            return false;        }});

This is best used when you have to search across a listview and have to filter out items. I never go by implementing the search function using the manifest file. The 2 methods do all the job.