Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Dialog is not called onSearchRequested()

I have been stuck on this for a little while now. I have looked at other answers on SO but they did not help me solve my problem.

My problem is that the search dialog is not invoked by onSearchRequested(). I am not able to figure out what is going on with my search...

The relevant code is as below...

Manifest

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

/res/xml/searchable.xml

<?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" >

</searchable>

I have the string resources defined in strings.xml.

SearchableActivity class

public class SearchableActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState) { 
          super.onCreate(savedInstanceState); 
          Log.d("SEARCH", "HERE");
          handleIntent(getIntent()); 
       } 

       public void onNewIntent(Intent intent) { 
          setIntent(intent); 
          handleIntent(intent); 
       } 

       public void onListItemClick(ListView l, View v, int position, long id) { 
          // call the appropriate detail activity
       } 

       private void handleIntent(Intent intent) { 
          if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
             String query = intent.getStringExtra(SearchManager.QUERY); 
             doSearch(query); 
          } 
       }    

       private void doSearch(String queryStr) { 
           Toast.makeText(getApplicationContext(), queryStr, Toast.LENGTH_LONG).show();
       } 

}

The piece of code in the activity that is invoking the search...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getTitle().equals("Search")) {
        Toast.makeText(getApplicationContext(), "Search = "+onSearchRequested(), Toast.LENGTH_LONG).show();
        return onSearchRequested();
    } else {
        return false;
    }
}

onSearchRequested() is returning true. But the SearchableActivity is not called. Any help is appreciated.

Thank you very much.


UPDATED

Never mind! I figured it out!

The solution was to move the meta-data outside the activity tag and inside the application tag. Have added below as an answer for those who run into this problem.

Thanks.

like image 951
neo108 Avatar asked Jun 26 '13 04:06

neo108


People also ask

How to start activity in android?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How to finish activity in android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

What is activity on android?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.


2 Answers

You need to declare FULL path to Search activity

<meta-data
          android:name="android.app.default_searchable"
          android:value="com.example.search.SearchableActivity" />
like image 80
Deni Erdyneev Avatar answered Oct 06 '22 07:10

Deni Erdyneev


You don't have to move meta-data outside activity tag unless you want every activity in your application to provide the search dialog. The reason it probably didn't work for you is because you put meta-data to wrong activity and moving it outside resolved the problem.

Do not confuse ResearchActivity with Activity that calls it like this:

    <activity
        android:name=".SearchableActivity"
        android:label="@string/title_activity_searchable"
        android:launchMode="singleTop">
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

It gives no error, but doesn't work. Working example:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.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>

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />
    </activity>
    <activity
        android:name=".SearchableActivity"
        android:label="@string/title_activity_searchable"
        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>

MainActivity is the activity that provides search dialog(it can be also search widget) and SearchableActivity is activity starting after user requests search and onSearchRequested() is invoked.

like image 37
Tomasz Mularczyk Avatar answered Oct 06 '22 06:10

Tomasz Mularczyk