Android FirestoreUI FirestoreRecyclerAdapter: Runtime Replace Query

April 8, 2019

Since FirestoreRecyclerAdapter doesn’t support change of query, so we need to recreate the adapter when Query changed.

Since FirestoreRecyclerAdapter use addSnapshotListener, we need to call adapter.stopListening (and lifecycle.removeObserver if setLifecycleOwner is used) before replacing the adapter.

class MyActivity:  AppCompatActivity() {

    private lateinit var adapter: LocalAdapter

    private fun createAdapter(): LocalAdapter {
        if (::adapter.isInitialized) {
            // call this before replacing the adapter to prevent invalid snapshot

        val query = ...

        // Quote is Model
        val builder = FirestoreRecyclerOptions.Builder<Quote>()
            .setQuery(query, object : SnapshotParser<Quote> {
                override fun parseSnapshot(snapshot: DocumentSnapshot): Quote {
                    return Quote.toObject(snapshot)!!.also {

        val options =
        return FirestoreQuoteAdapter(options, viewModel).also {
            adapter = it

    private fun initUi() {
        recyclerView.adapter = createAdapter()

NOTE: Refer to Android Firestore RecyclerView With FirestoreUI FirestoreRecyclerAdapter for Quote and FirestoreQuoteAdapter implementation.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.