I'm learning Android development and trying to add recent query suggestions following official documentation: Link. When i launch the app, there are no suggestions at all. Search by itself is working just fine. My searchView is located in a toolbar, the whole app is made of single activity and fragment. I managed to do it using RecyclerView and SharedPreferences, but it looks not so elegant and pretty and requires refactoring of my old code. I've seen related question, but nothing really answers mine.
manifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".PhotoGalleryApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PhotoGallery"
tools:targetApi="31">
<provider
android:name="com.vortex.android.photogallery.SuggestionProvider"
android:authorities="com.vortex.android.photogallery.SuggestionProvider"
android:exported="false"
android:enabled="true" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="TEST"
android:searchSuggestAuthority="com.vortex.android.photogallery.SuggestionProvider"
android:searchSuggestSelection=" ?">
</searchable>
SuggestionProvider.kt:
class SuggestionProvider: SearchRecentSuggestionsProvider() {
init {
setupSuggestions(AUTHORITY, MODE)
}
companion object {
const val AUTHORITY = "com.vortex.android.photogallery.SuggestionProvider"
const val MODE: Int = DATABASE_MODE_QUERIES
}
}
My toolbar layout:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_item_search"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="ifRoom" />
<item android:id="@+id/menu_item_clear"
android:title="@string/clear_search"
app:showAsAction="never" />
</menu>
Part of my code from Fragment class:
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate (R.menu.fragment_photo_gallery, menu)
val searchItem: MenuItem = menu.findItem(R.id.menu_item_search)
searchView = searchItem.actionView as? SearchView
val searchManager = requireContext().getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchView?.setSearchableInfo(searchManager.getSearchableInfo(activity?.componentName))
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
...
handleSearchQuery(query)
if (query != photoGalleryViewModel.galleryItem.value.query) {
binding.progressBar.visibility = View.VISIBLE
binding.photoGrid.visibility = View.INVISIBLE
photoGalleryViewModel.setQuery(query ?: "")
}
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
}
private fun handleSearchQuery(query: String?) {
query?.let {
SearchRecentSuggestions(
requireContext(),
SuggestionProvider.AUTHORITY,
SuggestionProvider.MODE
).saveRecentQuery(it, null)
}
}