Introduction
In the previous article we learned how to make a SearchView element in the activity or on the search bar located above at the top of the activity. Let us use the same example as we encountered in the previous article of mine.
Let us see an example in which we have a dictionary application. When we begin the search in the dictionary search provider tab, there are suggestions based on recent searches or after five or four search results we have seen that what happens is when the user types a word again into that space then the previous searched keyword is shown below the typing space as in the form of a list.
Using the Search Dialog
The Search Dialog provides a better way to create a search box at the top of the activity as previously said. The search dialog can do the same task as a SearchView was doing in our previous article we saw and analyzed the properties of the SearchView.
The search dialog is always hidden by default, until the user activates it. By invoking the method onSearchRequestd() we can activate our application. However this method works only after we enable the search dialog to the activity. Generally the way we follow to enable the search dialog in the activity it must receive the search queries and save them.
To declare the searchable activity for an activity search dialog add a <meta-data> element inside the respective element. We must create a class having name and attribute "android:name" and attribute value "android.app.default_searchable".
Let us create an activity.
Code
- <application ... >
-
- <activity android:name=".SearchableActivity" >
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <meta-data android:name="android.app.searchable"
- android:resource="@xml/searchable"/>
- </activity>
-
- <!-- this activity enables the search dialog to initiate searches
- in the SearchableActivity -->
- <activity android:name=".OtherActivity" ... >
-
- <meta-data android:name="android.app.default_searchable"
- android:value=".SearchableActivity" />
- </activity>
- ...
- </application>
Analysis The OtherActivity now includes a <meta-data> element to declare which searchable activity to use for searches, the activity has enabled the search dialog. While the user is in this activity, the
onSearchRequested() method activates the search dialog. When the user executes the search, the system starts SearchableActivity and delivers it the
ACTION_SEARCH intent. However if we want that every activity then something.
Invoking the search dialogGenerally some device has been providing a dedicated search button and the behavior of the button may vary between devices and many devices do not provide a search Button at all. A button must exist and when clicked the method onSearchRequested() is executed. It can also enable "type-to-search" functionality that must activate the search dialog when the user starts typing on the keyboard, the keystrokes are inserted into the search dialog. You can enable type-to-search in your activity by calling
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL) meanwhile the
activity's onCreate() method does something.
Impact on Activity lifecycleThe search dialog floats at the top of the screen. It does not cause any change in the activity. No life cycle methods are called. The activity might loose the focus of the inputis being given by the user. In some cases it happens when the activity looses the focus that the input is activated by just overriding the onSearchRequest().
For example
- @Override
- public boolean onSearchRequested() {
- pauseSomeStuff();
- return super.onSearchRequested();
- }
However a user skips the search by pressing the back button. In this case the activity regains input by closing the search dialog. Basically many things could be used, just like setOnDissListener() must be called for notifying the search dialog.
Let us set the
android: launchmode attribute to
"singleTop", then the searchable activity receives the Action_Search intent with a call to onNewIntent(intent);
- package com.example.valen;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
-
-
- public class RecentActivity extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.search);
- handleIntent(getIntent());
- }
-
- @Override
- protected 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);
- doMySearch(query);
- }
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.recent, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
When our system calls for the onNewIntent(Intent), the activity has not been restarted yet, then the getIntent() method returns the same intent that was received with onCreate(). That is a very good reason to create this intent here. This is why it should call
setIntent(Intent) inside
onNewIntent(Intent).
ing Search Context DataIn some cases there are refined searches done by the user so in these scenarios the necessary and extra data could be ed in the intents. It can be ed in the
APP_DATA Bundle that is included in the
ACTION_SEARCH intent.
To 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;
- }
- //However while doing all the things don't forget to add the following method.
- Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
- if (appData != null) {
- boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
- }
SummaryThis articles explains how to make a SearchView using the search dialog as said above. This article is a continuation of the previous articles however the basics of the search system has already been discussed so far.