Searchable activity being called twice

android, search

Solution

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.

Problem

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"/> ```

Original source