Implementing search with multiple fragments MVVM

18
September 03, 2019, at 12:30 PM

I am trying to build an application which has multiple tabs in single Activity (using TabLayout and fragments for each tab). My question is how to properly transfer search query from activity's SearchView to the corresponding selected fragment. Only the selected fragment should complete the search action.

One way of accomplishing this task is to have a separate SearchView in each fragment. Also considering that fragment has its own ViewModel, we can just update query and observe changes. By the way, search action for each fragment is different.

example:

Fragment1.kt | Fragment2.kt | ...


    private lateinit var searchViewModel: SearchViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initializeUi()
        initializeViewModels()
        searchViewModel.getSuggestions().observe(this, Observer(this::populateSuggestions))
    }
    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater){
        menuInflater.inflate(R.menu.menu_main, menu)
        val searchView = menu?.findItem(R.id.menu_search)?.actionView as SearchView
        searchView.queryHint = getString(R.string.label_search)
        searchView.setOnQueryTextListener(onQueryTextListener())
    }
    private fun onQueryTextListener() = object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            return false
        }
        override fun onQueryTextChange(newText: String?): Boolean {
            if (newText.isNullOrBlank()) return false
            if (newText.length >= 3) {
                searchViewModel.setQuery(newText)
            }
            return false
        }
    }

But I am wondering if it is possible to make search and transfer the query to only selected tab from Activity which contains these fragments. There can be a single ViewModel in the activity which is shared by those fragments. Activity updates the LiveData<String> query in the ViewModel as user types in the SearchView. But in this case, there is an overhead in limiting search action to the particular selected tab. Would be nice to have a look at different approaches if there are any.

READ ALSO
How to query all of the saved phone numbers, including those exported from external apps, in Android?

How to query all of the saved phone numbers, including those exported from external apps, in Android?

I'm trying to query all of the phone numbers for a contact on Android, currently I'm showing the contacts using the following method:

39
Change the item index in an arraylist

Change the item index in an arraylist

I have a List<POJO> with three PackagenamesSo if I "click" on the second item com

43
Unity IL2CPP Android build crashes Mac

Unity IL2CPP Android build crashes Mac

When trying to build a Google App Bundle, every time the process gets to the step that says Building Native Binaries With IL2CPP I get a “Your system has run out of application memory” error and my MacBook reboots

23
Android Retrofit: async loading for model data to be filled into spinner issue

Android Retrofit: async loading for model data to be filled into spinner issue

so I'll be straight forward, I have a model class, that will be loading from the database using retrofit asynchronously, the data loaded will be displayed in a spinnerthe issue is: since it's loading asynchronously, the spinner is being filled with an empty arraylist,...

53