diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index a87035c66a..38cffeb8d8 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -58,6 +58,7 @@ class MainActivity : BaseActivity() { private val subscriptionsViewModel: SubscriptionsViewModel by viewModels() private var savedSearchQuery: String? = null + private var shouldOpenSuggestions = true override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -283,6 +284,8 @@ class MainActivity : BaseActivity() { } override fun onQueryTextChange(newText: String?): Boolean { + if (!shouldOpenSuggestions) return true + // Prevent navigation when search view is collapsed if (searchView.isIconified || binding.bottomNav.menu.children.any { @@ -356,6 +359,15 @@ class MainActivity : BaseActivity() { return super.onCreateOptionsMenu(menu) } + /** + * Update the query text in the search bar without opening the search suggestions + */ + fun setQuerySilent(query: String) { + shouldOpenSuggestions = false + searchView.setQuery(query, false) + shouldOpenSuggestions = true + } + override fun onOptionsItemSelected(item: MenuItem): Boolean { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt index bb989cf8a8..cb0acb01e8 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt @@ -23,6 +23,7 @@ import com.github.libretube.extensions.TAG import com.github.libretube.extensions.hideKeyboard import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.helpers.PreferenceHelper +import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.adapters.SearchAdapter import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.util.TextUtils @@ -60,6 +61,10 @@ class SearchResultFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + // fixes a bug that the search query will stay the old one when searching for multiple + // different queries in a row and navigating to the previous ones through back presses + (context as MainActivity).setQuerySilent(query) + binding.searchRecycler.layoutManager = LinearLayoutManager(requireContext()) // add the query to the history