Avoid hiding emptyView when rotate the screen at very first time

I am displaying an emptyView at very first time that app starts. I hide this View as soon as user searches :

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                
                override fun onQueryTextChange(query: String): Boolean {
                    if (query.isNotEmpty()) {
                        search(query)
                    }
                    emptyLayout.visibility = View.INVISIBLE
                    return true
                }
            })

private fun search(query: String) {
        if (viewModel.showQuery(query)) {
            binding.recyclerView.scrollToPosition(0)
            (binding.recyclerView.adapter as MainAdapter).submitList(null)
        }
    }

And here is viewModel and showQuery method :

class SearchViewModel @Inject constructor(
    private val searchPeopleUseCase: SearchPeopleUseCase,
    private val schedulerProvider: BaseSchedulerProvider,
    private val app: Application,
) : BasePagingViewModel<Character>(app, schedulerProvider) {

    private val query = MutableLiveData<String>()

    override val repoResult: LiveData<Listing<Character>> = Transformations.map(query) {
        SearchPageKeyRepository(
            searchPeopleUseCase, it, compositeDisposable,
            schedulerProvider, app.applicationContext
        ).getItems()
    }

    fun showQuery(query: String): Boolean {
        if (this.query.value == query) {
            return false
        }
        this.query.value = query
        super.showQuery()
        return true
    }
}

When I rotate the device at very first time which app starts, emptyView becomes invisible since onQueryTextChange get called as soon as screen rotates. I wonder if there is any solution to avoid hiding emptyView when screen rotates and search text is empty at very first time?

Full source code of sample can be found here : https://github.com/Ali-Rezaei/StarWarsSearch-RxPaging

Answer

One way to handle it that let OS know that you will be handling the configuration changes for certain behaviors . below is the configuration worked for me.

android:configChanges="keyboard|keyboardHidden|screenSize|orientation"

I humbly suggest you to take a look at This thread to know its Proc and cons.

Addenda :

Another option as mentioned by responder in the comments is using savedInstanceState :

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.putBoolean(EMPTY_VIEW_KEY, _binding?.recyclerView?.adapter?.itemCount != 0)
}

And I check it in onQueryTextChange in order to hide emptyView :

override fun onQueryTextChange(query: String): Boolean {
   if (query.isNotEmpty()) {
      search(query)
   }
   if (query.isNotEmpty() || savedInstanceState == null || savedInstanceState.getBoolean(EMPTY_VIEW_KEY)) {
      emptyLayout.visibility = View.INVISIBLE
   }
   return true
}