Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searchable activity being called twice

Tags:

android

search

I'm trying to create my first Android app and in the process of adding to it SEARCH functionality. I've followed the Android Developer documentation to add both the Search dialog and widget. Unfortunately, whenever I perform a search, the "onCreate" AND the "onNewIntent" of the search Activity are called. That is, by typing something in the Action Bar Search box and hitting ENTER, the search is called TWICE. I'm stuck. Is some global flag supposed to be returned from the Searchable activity notifying the app that the search has been completed? Are BOTH the Search dialog AND widget being called?

I've searched previous posts on this site and on the web to no avail. Thank you for any help.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shop"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name="com.shop.RestIntentService" />

        <activity
            android:name="com.shop.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="com.shop.CatalogActivity"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.shop.SearchableActivity"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <!--
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
            -->

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <meta-data
            android:name="android.app.default_searchable"
            android:value="com.shop.SearchableActivity" />

    </application>

</manifest>

SearchableActivity.java

 public class SearchableActivity extends ListActivity implements Receiver {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate");
        handleIntent(getIntent());
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        System.out.println("onNewIntent");
        setIntent(intent);
        handleIntent(intent);
    }

    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) {
        System.out.println("searching..." + queryStr);
    }

CatalogActivity.java

@Override   
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.sample, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));

        //return super.onCreateOptionsMenu(menu);
        return true;
    }   

sample.xml

<item
    android:id="@+id/menu_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/menu_search"/>
like image 560
user2199730 Avatar asked Mar 26 '13 09:03

user2199730


2 Answers

I think methods onNewIntent and onCreate are mutually exclusive. You set launchMode to "singleTop", so the method onNewIntent would be called and not the onCreate.

I have a similar problem when one search request is processed twice. Working with the SearchableDictionary sample in the WXGA 10.1 tablet emulator I found that the first search call works fine, but consequent calls create two SEARCH events, so they are processed twice. Somebody mentioned about a bug in Ti. (http://developer.appcelerator.com/question/127166/android-search-keyboardtype-fires-return-event-twice )

I tested apps on a real Samsung tablet and I didn’t see two SEARCH events, so I guess it’s an emulator problem, not a code problem.

like image 99
user1853575 Avatar answered Nov 16 '22 04:11

user1853575


Try to remove super.OnNewIntent(intent) in your SearchableActivity.

According to your manifest you are trying to use search dialog

<meta-data
android:name="android.app.default_searchable"
android:value="com.shop.SearchableActivity" />

and in the same time you use SearchView in your Menu. If you use SearchView you should use

    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>

like in http://developer.android.com/guide/topics/search/searchable-config.html.

That is my example of xml/searchable.xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:label="@string/app_name"
    android:hint="@string/search"
    android:searchSuggestAuthority="@string/authority"
    android:searchSuggestSelection=" ?"
    android:voiceSearchMode="launchRecognizer">
</searchable>
like image 38
akelix Avatar answered Nov 16 '22 04:11

akelix