title: Layout doesn't update right away after deleting last item from Room database
body: <p>I have this fragment in which I store my 'favorite items' and I can delete them when I click on a button if I want to. The implementation works well until I get to the last item and it doesn't disappear unless I go to another fragment and then come back (as in, the item is deleted but the recycler view still shows it unless I update the fragment myself).</p>
  <p>How can I make the last item disappear right away? Setting notifyDataSetChanged() after the deleteHandler in the adapter does not seem to work.</p>
  <p>This is the fragment where I have the items:</p>
  <pre><code>class FavoritesFragment : Fragment() {
  
      private val mfavoriteViewModel by viewModels&lt;FavoriteViewModel&gt;()
      private lateinit var binding: FragmentFavoritesBinding
      private val deleteHandler: (Favorites) -&gt; Unit = {
          mfavoriteViewModel.deleteFavorite(it)
  
      }
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?,
      ): View {
          binding = FragmentFavoritesBinding.inflate(layoutInflater)
          //recyclerview
          val adapter = FavoritesAdapter(deleteHandler)
          binding.rvFavList.layoutManager = LinearLayoutManager(context)
          binding.rvFavList.adapter = adapter
  
          //favoriteViewModel
          mfavoriteViewModel.readAllData.observe(viewLifecycleOwner, { favorite -&gt;
  
              if (favorite.isEmpty()) {
                  binding.emptyState.text = getString(R.string.emptyState)
                  binding.emptyState.visibility = View.VISIBLE
              } else {
                  adapter.setData(favorite)
                  binding.emptyState.visibility = View.GONE
              }
          })
  
          return binding.root
      }
  }
  </code></pre>
  <p>The adapter:</p>
  <pre><code>class FavoritesAdapter(val deleteHandler: (Favorites) -&gt; Unit) :
      RecyclerView.Adapter&lt;FavoritesAdapter.ViewHolder&gt;() {
  
      private var favoriteList = emptyList&lt;Favorites&gt;()
  
      class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          private val binding = FavItemBinding.bind(itemView)
  
          val favTitle: TextView = binding.tvFavsTitle
          val favItem: ImageButton = binding.btnFavs
          val favImg: ImageView = binding.ivFavs
  
          fun bind(favorites: Favorites) {
              Picasso.get().load(favorites.image).into(favImg)
              favTitle.text = favorites.title
          }
  
      }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
          return ViewHolder(
              LayoutInflater.from(parent.context).inflate(R.layout.fav_item, parent, false)
          )
      }
  
      override fun onBindViewHolder(holder: ViewHolder, position: Int) {
          holder.bind(favoriteList[position])
  
          //delete favorite item
          holder.favItem.setOnClickListener {
              deleteHandler(favoriteList[position])
          }
  
      }
  
      override fun getItemCount(): Int {
          return favoriteList.size
      }
  
      fun setData(favorite: List&lt;Favorites&gt;) {
          this.favoriteList = favorite
          notifyDataSetChanged()
      }
  
  }
  </code></pre>
  <p>This is the favorite's viewmodel:</p>
  <pre><code>class FavoriteViewModel(application: Application) : AndroidViewModel(application) {
  
      val readAllData: LiveData&lt;List&lt;Favorites&gt;&gt;
      private val repository: FavoritesRepository
  
      init {
          val favoriteDao = FavoriteDatabase.getDatabase(application).favoriteDao()
          repository = FavoritesRepository(favoriteDao)
          readAllData = repository.readAllData
      }
  
      fun addFavorite(favorite: Favorites) {
          viewModelScope.launch(Dispatchers.IO) {
              repository.addFavorite(favorite)
          }
      }
  
      fun deleteFavorite(favorite: Favorites) {
          viewModelScope.launch(Dispatchers.IO) {
              repository.deleteFavorite(favorite)
          }
      }
  
      fun deleteAllFavorites() {
          viewModelScope.launch(Dispatchers.IO) {
              repository.deleteAllFavorites()
          }
      }
  }
  </code></pre>