I have a search bar, after searching in the search bar, if I click back button the app closes. What I want is if I click back button after searching in search bar the default list must be shown.
activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(){
}else{
isEnabled = false
activity?.onBackPressed()
}
}
})
searchBar.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
val listData = arrayListOf<Restaurant>()
for (i in 0 until restaurantInfoList.size) {
if (restaurantInfoList[i].restaurantName.toLowerCase()
.contains(searchBar.text.toString().toLowerCase()) ||
restaurantInfoList[i].restaurantPrice.contains(searchBar.text.toString()) ||
restaurantInfoList[i].restaurantRating.contains(searchBar.text.toString())
) {
listData.add(restaurantInfoList[i])
}
}
recyclerDashboard.adapter = RestaurantRecycleAdapter(activity as Context, listData)
(recyclerDashboard.adapter as RestaurantRecycleAdapter).notifyDataSetChanged()
}
}
)
2
Answers
The app is closing because you have called the activity’s onbackPressed() method. Unless you handle the callback on your own, it will close.
In your backpressed method, check if your search bar is empty and produce the default list in the adapter.
Remove this line in else condition