title: Recyclerview dataset does not change on fragment start
body: <p>I have a RecyclerView on my fragment that gets its data from an ArrayList in an object named Datasource. I want to get new items from Cloud Firestore when fragment gets started, so I created a function named getAllProducts however I can only get items when I click the bottom navigation bar.</p>
  <p>I almost tried every answer to another question. I tried to reload fragment onCreate, I created a navigation action to fragment itself, I added a function in RecyclerView adapter that gets data from getAllProducts function and notifyDataSetChanged(). I make getAllProduct returns array list and used it as data in RecyclerView. None of them worked.</p>
  <p>When app is started:</p>
  <p><a href="https://i.stack.imgur.com/gb5GV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gb5GV.png" alt="App started" /></a></p>
  <p>When clicked home:</p>
  <p><a href="https://i.stack.imgur.com/K0kzd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K0kzd.png" alt="Clicked home" /></a></p>
  <p>Here related code</p>
  <p>ProductAdapter.kt</p>
  <pre><code>class ProductAdapter(private val productList: ArrayList&lt;ProductItem&gt;):RecyclerView.Adapter&lt;ProductViewHolder&gt;() {
  
      inner class ProductViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
          val productImage: ImageView = itemView.findViewById(R.id.productPhoto)
          val productName: TextView = itemView.findViewById(R.id.productName)
          val productRatingBar: RatingBar = itemView.findViewById(R.id.ratingBar2)
          val productRating: TextView = itemView.findViewById(R.id.ratingint)
          val storeName: TextView = itemView.findViewById(R.id.productStoreName)
          val productPrice: TextView = itemView.findViewById(R.id.productPrice)
  
      }
  
  
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
  
          val itemView = LayoutInflater.from(parent.context).inflate(R.layout.product_item,
          parent,false)
  
          return ProductViewHolder(itemView)
      }
  
      override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
          val currentItem = productList[position]
          holder.itemView.setOnClickListener {
                  view -&gt;
              val action = ProductFragmentDirections.actionProductFragmentToParticularProductFragment2(currentItem)
              view.findNavController().navigate(action)
          }
          holder.productImage.setImageResource(currentItem.productImageResource)
          holder.productName.text = currentItem.productName
          holder.productRatingBar.rating = currentItem.productRating.toFloat()
          holder.productRating.text = currentItem.productRating.toString()
          holder.storeName.text = currentItem.storeName
          holder.productPrice.text = currentItem.productPrice.toString().plus(&quot;$&quot;)
  
      }
  
  
      override fun getItemCount() = productList.size
  }
  </code></pre>
  <p>Datasource.kt</p>
  <pre><code>object Datasource {
  
      var productList = arrayListOf(
  
          ProductItem(R.drawable.attach_money,&quot;Sahibinden aerator&quot;,4.5,&quot;Serhat Yilmaz&quot;,8),
          ProductItem(R.drawable.email,&quot;dus kitapları&quot;,3.0,&quot;nerhat Yilmaz&quot;,9),
          ProductItem(R.drawable.passwordicon,&quot;h files&quot;,1.5,&quot;berhat Yilmaz&quot;,77),
          ProductItem(R.drawable.category,&quot;piyasemen&quot;,5.0,&quot;xerhat Yilmaz&quot;,55),
          ProductItem(R.drawable.attach_money,&quot;Sahibinden sasfaf&quot;,1.5,&quot;kerhat Yilmaz&quot;,11),
          ProductItem(R.drawable.email,&quot;tus kitapları&quot;,3.0,&quot;uerhat Yilmaz&quot;,21),
          ProductItem(R.drawable.passwordicon,&quot;f files&quot;,1.5,&quot;yerhat Yilmaz&quot;,47),
          ProductItem(R.drawable.category,&quot;anguldruva&quot;,2.0,&quot;terhat Yilmaz&quot;,35),
          ProductItem(R.drawable.email,&quot;tus2 kitaplarım&quot;,3.0,&quot;werhat Yilmaz&quot;,78),
          ProductItem(R.drawable.passwordicon,&quot;endobox&quot;,2.5,&quot;Zerhat Yilmaz&quot;,95),
          ProductItem(R.drawable.category,&quot;basplak&quot;,4.0,&quot;Ferhat Yilmaz&quot;,12)
  
      )
  
      fun loadProduct(): ArrayList&lt;ProductItem&gt;{
          return productList
      }
  }
  </code></pre>
  <p>ProductFragment.kt</p>
  <pre><code>class ProductFragment : Fragment() {
      private val myDataset = Datasource.productList
      private val adapter = ProductAdapter(myDataset)
  
  
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
  
  
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.products_fragment, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
  
          val recyclerView: RecyclerView = view.findViewById(R.id.recycler_product)
          recyclerView.adapter = adapter
          recyclerView.layoutManager = LinearLayoutManager(requireActivity())
          FireStoreClass().getAllProducts()
          adapter.notifyDataSetChanged()
  
      }
  
  
  
  }
  </code></pre>
  <p>FireStoreClass().getAllProducts()</p>
  <pre><code>private val mFireStore = FirebaseFirestore.getInstance()
  
  fun getAllProducts() {
  
  
      mFireStore.collection(Constants.PRODUCTS)
          .get()
          .addOnSuccessListener {
              documents -&gt;
              val list = arrayListOf&lt;ProductItem&gt;()
              for (document in documents){
  
                  val product = document.toObject(ProductItem::class.java)
                  list.add(product)
  
              }
              Datasource.productList = list
              }
          .addOnFailureListener {
              //Toast.makeText(activity, &quot;Can not get Product! ${it.message}&quot;, Toast.LENGTH_SHORT).show()
          }
  
  }
  </code></pre>
  <p>in case you need to see:</p>
  <p>NavigateActivity.kt</p>
  <pre><code>class NavigateActivity : AppCompatActivity() {
      private lateinit var navController: NavController
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_navigate)
          val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
          navController = navHostFragment.findNavController()
          val bottomNavigationView = findViewById&lt;BottomNavigationView&gt;(R.id.bottomNavView)
          bottomNavigationView.setupWithNavController(navController)
          //val action = HomeFragmentDirections
  
  
      }
  
  
  }
  </code></pre>
  <p>mobile_navigation.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:id=&quot;@+id/mobile_navigation&quot;
      app:startDestination=&quot;@id/product_Fragment&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/product_Fragment&quot;
          android:name=&quot;com.example.TheDentalSupplies.fragments.ProductFragment&quot;
          android:label=&quot;products_fragment&quot;
          tools:layout=&quot;@layout/products_fragment&quot; &gt;
  
          &lt;action
              android:id=&quot;@+id/action_productFragment_to_particularProductFragment2&quot;
              app:destination=&quot;@id/particularProductFragment&quot; /&gt;
  
          &lt;action
              android:id=&quot;@+id/action_productFragment_Reload&quot;
              app:destination=&quot;@id/product_Fragment&quot; /&gt;
  
  
  
      &lt;/fragment&gt;
      &lt;fragment
          android:id=&quot;@+id/addPostNavActivity&quot;
          android:name=&quot;com.example.TheDentalSupplies.AddActivity&quot;
          android:label=&quot;addpost&quot;
          tools:layout=&quot;@layout/addpost&quot; &gt;
          &lt;action
              android:id=&quot;@+id/action_addPostNavActivity_to_productFragment&quot;
              app:destination=&quot;@id/product_Fragment&quot; /&gt;
      &lt;/fragment&gt;
  
      &lt;activity
          android:id=&quot;@+id/userActivity&quot;
          android:name=&quot;com.example.TheDentalSupplies.UserActivity&quot;
          android:label=&quot;userlayout&quot;
          tools:layout=&quot;@layout/userlayout&quot; /&gt;
      &lt;fragment
          android:id=&quot;@+id/bucketFragment&quot;
          android:name=&quot;com.example.TheDentalSupplies.fragments.BucketFragment&quot;
          android:label=&quot;bucketlayout&quot;
          tools:layout=&quot;@layout/bucketlayout&quot; /&gt;
      &lt;fragment
          android:id=&quot;@+id/particularProductFragment&quot;
          android:name=&quot;com.example.TheDentalSupplies.fragments.ParticularProductFragment&quot;
          android:label=&quot;product_layout&quot;
          tools:layout=&quot;@layout/product_layout&quot;&gt;
          &lt;argument
              android:name=&quot;navProductItem&quot;
              app:argType=&quot;com.example.TheDentalSupplies.models.ProductItem&quot; /&gt;
          &lt;action
              android:id=&quot;@+id/action_to_bucketFragment&quot;
              app:destination=&quot;@id/bucketFragment&quot; /&gt;
          &lt;action
              android:id=&quot;@+id/action_to_messageActivity&quot;
              app:destination=&quot;@id/messageActivity&quot; /&gt;
          &lt;action
              android:id=&quot;@+id/action_feedbacksFragment2&quot;
              app:destination=&quot;@id/feedbacksFragment2&quot; /&gt;
      &lt;/fragment&gt;
      &lt;activity
          android:id=&quot;@+id/messageActivity&quot;
          android:name=&quot;com.example.TheDentalSupplies.MessageActivity&quot;
          android:label=&quot;messagelayout&quot;
          tools:layout=&quot;@layout/messagelayout&quot; /&gt;
      &lt;fragment
          android:id=&quot;@+id/feedbacksFragment2&quot;
          android:name=&quot;com.example.TheDentalSupplies.fragments.FeedbacksFragment&quot;
          android:label=&quot;feedbacks_layout&quot;
          tools:layout=&quot;@layout/feedbacks_layout&quot; /&gt;
  
  &lt;/navigation&gt;
  </code></pre>