'Last navigation gets resubmitted
Okay, so I'm not sure, what exactly triggers that. But I have a suspicion. Sadly it will get hard to make a MWE, since it is a very complex app with lots of fragments, but I'll try to explain as good as possible. So I got a DictionaryFragment, which contains one other fragment (AllVocabularyFragment). In the DictionaryFragment I have a search bar. The code for this one is the following. XML:
<SearchView
android:id="@+id/searchView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginEnd="16dp"
android:background="@drawable/searchview_background"
android:iconifiedByDefault="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<requestFocus/>
</SearchView>
In the code:
val searchView = view.findViewById<SearchView>(R.id.searchView)
searchView?.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
if(!query.isNullOrBlank())
searchForValue(query)
Log.d("FIXING", "Query submitted")
//So they close the keyboard for us
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
In the AllVocabularyFragments there's some buttons you can tap on that lead you to another fragment (ListForRangeFragment) via the NavigationController:
//Transfer to fragment with vocabulary list
override fun onMenuItemClick(item: MenuItem?): Boolean {
return if(item!=null) {
val navController = findNavController(itemView)
val action = DictionaryFragmentDirections.showVocabularyList(
vocabularyList.language,
vocabularyList.name + " " + getTitleForLetterRange(vocabularyList.subdivisions[item.itemId]),
vocabularyList.subdivisions[item.itemId])
navController.navigate(action)
true
}else {
false
}
}
In the following fragment there's a list of words and when you tap on one it redirects you to the DictionaryFragment, enters the word for you and searches for it, again via the NavigationController:
val navController = Navigation.findNavController(holder.itemView)
val action = VocabularyListForLanguageFragmentDirections.searchTappedWord(word)
navController.navigate(action)
This replaces the AllVocabularyFragment inside of the DictionaryFragment with another fragment that shows the search results. So far so good. That's how it's supposed to act. But now comes the weird thing: After that I press the back button, it brings back the AllVocabularyFragment inside of the DictionaryFragment, query still stays filled (supposed to be like that, I never emptied it). Then I again use the buttons to get to the ListForRangeFragment. I press the back button again. And now here's the weird thing: It goes back to the DictionaryFragment and resubmits the query without me pressing the submit button. It seems as if it was stepping back in the NavigationController actions and redid them. Or the OnQuerySubmit gets called on initialization of the SearchBar. I'm not sure where exactly that bug is coming from. But I would very much appreciate any ideas on it.
EDIT: Pressing the back button further it indeed goes back the whole search history and history of lists. So I guess it has to do something with the NavigationController.
EDIT 2: So, it clearly calls again the last navigation that happened in the NavController. It does not do so directly, cause the navigation drawer template adds up some other fragments on the backstack somewhere along the line, but when ever you end up in the ListForRangeFragment, that seems to be the last thing on backstack. I tried to clear the backstack with popBackstack(R.id.mobile_navigation, true), hoping that then it would only leave the backstack of the navigation drawer and do it's thing as it should, but it cleared the whole backstack. Pressing back then closes the whole app. So I wonder how I can remove only the last entry of the backstack and keep all other.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|