title: RecyclerView load after doing action and not as the fragment loads
body: <p>I've been searching in google for some time now for a solution to my problem but I can't find any.
  My <code>RecyclerView</code> getItemCount() returns 0 and only after I switch between few fragments and get back to the fragment I use the <code>RecyclerView</code> in the adapter loads and return the real item number.
  I did put logger inside my getList function and even tho the items are pushed inside my array the command array. size return 0... I really can't find solution here.</p>
  <p>this is some code to work with
  BTW -&gt; first time i use FireBaseDataBase when i used SQLite i did not had problem with recylcer view.
  if u need more code lemme know
  many thanks! people help me here :)
  fragment code:</p>
  <pre><code>package com.seanCo.DeliveryApp.fragments
  
  import android.app.Activity
  import android.app.AlertDialog
  import android.content.Context
  import android.content.DialogInterface
  import android.graphics.drawable.Drawable
  import android.os.Build
  import android.os.Bundle
  import android.os.Parcel
  import android.os.Parcelable
  import android.renderscript.Sampler
  import android.util.Log
  import androidx.fragment.app.Fragment
  import android.view.LayoutInflater
  import android.view.View
  import android.view.View.inflate
  import android.view.ViewGroup
  import android.widget.ImageButton
  import android.widget.TextView
  import androidx.appcompat.app.AppCompatActivity
  import androidx.appcompat.widget.AlertDialogLayout
  import androidx.core.content.res.ColorStateListInflaterCompat.inflate
  import androidx.recyclerview.widget.LinearLayoutManager
  import androidx.recyclerview.widget.RecyclerView
  import com.google.firebase.database.DataSnapshot
  import com.google.firebase.database.DatabaseError
  import com.google.firebase.database.DatabaseReference
  import com.google.firebase.database.ValueEventListener
  import com.seanCo.DeliveryApp.R
  import kotlinx.android.synthetic.main.adding_delivery.*
  import kotlinx.android.synthetic.main.adding_delivery.view.*
  import kotlinx.android.synthetic.main.fragment_active_delivery.*
  import java.sql.Date
  import java.sql.Time
  import java.text.SimpleDateFormat
  import java.time.LocalDateTime
  import java.time.LocalTime
  import java.time.format.DateTimeFormatter
  
  class activeDeliveryFragment(db : DatabaseReference) : Fragment() {
  
      val tempDB = db
      var deliveryID = 0
      var tempList : MutableList&lt;deliveryInfo&gt; = ArrayList()
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
  
          val cView = inflater.inflate(R.layout.fragment_active_delivery, container, false)
          val tView= activity
  
          findId()
          val toolbarTitle = tView?.findViewById&lt;TextView&gt;(R.id.toolbar_title)
          val toolbar = tView?.findViewById&lt;androidx.appcompat.widget.Toolbar&gt;(R.id.toolbar)
          val text = toolbarTitle?.setText(&quot;משלוחים פעילים&quot;)
  
          val delivery = deliveryInfo()
          val addButton = cView.findViewById&lt;ImageButton&gt;(R.id.plus_button)
          addButton.setOnClickListener{
  
              val dialog = AlertDialog.Builder(getContext())
              val dialogView = layoutInflater.inflate(R.layout.adding_delivery, null)
              dialog.setView(dialogView)
              val dialogData = dialog.show()
  
              dialogView.dialog_pos.setOnClickListener{
                  dialogData.dismiss()
                  delivery.address = dialogData.et_address.text.toString()
                  val timer = setTime((dialogData.et_timer.text.toString()).toInt())
                  delivery.timer = timer
                  deliveryID++
                  val array : Array&lt;Any&gt; = arrayOf(delivery.address, delivery.timer)
                  Log.i(&quot;tag&quot;, delivery.timer.toString())
                  tempDB.child(&quot;activeDeliveryies/$deliveryID&quot;).child(&quot;addess&quot;).setValue(delivery.address)
  
  
              }
  
              dialogView.dialog_neg.setOnClickListener{
                  dialogData.dismiss()
              }
  
          }
  
  
          return cView
      }
  
      override fun onResume() {
          refreshList()
          return super.onResume()
      }
  
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
  
          rv_activeDelivery.layoutManager = LinearLayoutManager(activity)
          return super.onActivityCreated(savedInstanceState)
      }
  
  
      fun refreshList(){
          getList()
          rv_activeDelivery.adapter = rvAdapater(requireContext(), this, tempList)
      }
  
      fun getList(){
          tempDB.child(&quot;activeDeliveryies&quot;).addValueEventListener( object : ValueEventListener{
              override fun onCancelled(error: DatabaseError) {
              }
  
              override fun onDataChange(snapshot: DataSnapshot) {
                  for(id in snapshot.children){
                      val temp = id.key.toString().toInt()
                      if(temp &gt; tempList.size) {
                          for (address in snapshot.child(id.key.toString()).children) {
                              val infoList = deliveryInfo()
                              infoList.address = address.getValue().toString()
                              tempList.add(infoList)
                              Log.i(&quot;first&quot;, address.getValue().toString())
  
                          }
                      }
                  }
  
              }
          })
  
      }
  
      fun setTime(timer : Int) : Int{
         if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
             var getTime = LocalDateTime.now()
             var hours = getTime.hour
             var minutes = getTime.minute + timer
             if(minutes &gt;= 60){
                 minutes -= 60
                 hours += 1
             }
               val newTime = &quot;$hours$minutes&quot;
             return newTime.toInt()
          } else {
  
             var date = java.util.Date()
             val formatter = SimpleDateFormat(&quot;HH:mm&quot;)
             val answer: String = formatter.format(date)
             return 0
          }
  
      }
  
      fun findId(){
          tempDB.child(&quot;activeDeliveryies&quot;).addValueEventListener( object : ValueEventListener{
              override fun onCancelled(error: DatabaseError) {
                  TODO(&quot;Not yet implemented&quot;)
              }
  
              override fun onDataChange(snapshot: DataSnapshot) {
  
                  for(id in snapshot.children){
                      deliveryID = (id.key.toString()).toInt()
  
                  }
              }
          })
      }
  
      class rvAdapater(val context : Context,val activeDelivery : activeDeliveryFragment,val list : MutableList&lt;deliveryInfo&gt;) : RecyclerView.Adapter&lt;rvAdapater.ViewHolder&gt;(){
          override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
              return ViewHolder(LayoutInflater.from(activeDelivery.context).inflate(R.layout.rv_child_activedelivery, parent, false))
          }
  
          override fun getItemCount(): Int {
              Log.i(&quot;tag&quot;, list.size.toString())
              return list.size
          }
  
          override fun onBindViewHolder(holder: rvAdapater.ViewHolder, position: Int) {
              holder.deliveryAddress.text = list[position].address
          }
  
          class ViewHolder(v : View) : RecyclerView.ViewHolder(v){
  
              val deliveryAddress : TextView = v.findViewById(R.id.tv_address)
  
          }
      }
  
  
  }
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <pre><code>    fun refreshList(){
          getList()
          rv_activeDelivery.adapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
              override fun onChanged() {
                  super.onChanged()
                  Log.d(&quot;frag&quot;, &quot;numberOfItems: &quot; + rv_activeDelivery.adapter?.getItemCount())
              }
          })
          rv_activeDelivery.adapter = rvAdapater(requireContext(), this, tempList)
          (rv_activeDelivery.adapter as rvAdapater).notifyDataSetChanged()
  
      }
  </code></pre>
  <p><strong>UPDATE: Found solution:</strong>
  i just changed the order of the commands inside refreshList method</p>
  <pre><code>  fun refreshList(){
          val con = this
          tempDB.child(&quot;activeDeliveryies&quot;).addValueEventListener( object : ValueEventListener{
              override fun onCancelled(error: DatabaseError) {
              }
  
              override fun onDataChange(snapshot: DataSnapshot) {
  
                  Log.i(&quot;datachanged&quot;, &quot;data&quot;)
                  for(id in snapshot.children){
                      val temp = id.key.toString().toInt()
                      if(temp &gt; tempList.size) {
                          for (address in snapshot.child(id.key.toString()).children) {
                              val infoList = deliveryInfo()
                              infoList.address = address.getValue().toString()
                              tempList.add(infoList)
  
                          }
                      }
                  }
                  rv_activeDelivery.adapter = rvAdapater(requireContext(), con, tempList)
                  val temp = (rv_activeDelivery.adapter as rvAdapater)
                  
              }
          })
  
  
      }
  </code></pre>