OnSuggestionClick recreates my activity

In my application, I made my content provider searchable by specifying my main action as the default search activity for my manifest.

<activity android:name="com.pt.application.MainActivity" android:label="@string/title_activity_map" android:launchMode="singleTop" android:theme="@style/AppTheme" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> 

Then I applied the onSuggestionClick option to update the map based on the offer clicked by the user. Searchview is in the action bar. The problem is that when the user clicks on the sentence, the activity is somehow recreated (I think that OnPause and OnResume are called?). Is there any way to prevent this? I don’t want my activity to be β€œrecreated” every time a user clicks on an offer.

If more code is required, please ask. Thanks.

EDIT: code added

  <?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" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" android:voiceLanguageModel="web_search" android:searchSuggestAuthority="com.pt.provider" android:searchSuggestIntentData="content://com.pt.provider/busstop"> </searchable> 

In my main activity:

  // Bind the Activity's SearchableInfo to the Search View searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView(); this.setupSearchView(); private void setupSearchView() { // Use the Search Manager to find the SearchableInfo related // to this Activity. SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE); SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName()); searchView.setSearchableInfo(searchableInfo); searchView.setSubmitButtonEnabled(false); searchView.setOnSuggestionListener(new OnSuggestionListener() { @Override public boolean onSuggestionSelect(int position) { return false; } @Override public boolean onSuggestionClick(int position) { // Do things return false; } }); } 
+6
source share
1 answer

Solved my question. Changed in manifest:

From:

 android:launchMode="singleTop" 

To:

 android:launchMode="singleTask" 
+2
source

All Articles