How to prevent fragments from being recreated when using BottomNavigationView with Navigation component?

Viewed 721

I have an app, one activity, with BottomNavigationView and Fragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

And four fragments, I navigate like this, in MainActivity:

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        binding.navView.setupWithNavController(navController)

        binding.navView.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_fra1 -> {
                    findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.Fragment)
                }
                R.id.navigation_fra2 -> {
                    findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.Fragment2)
                }
                R.id.navigation_fra3 -> {
                    findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.Fragment3)
                }
                R.id.navigation_fra4 -> {
                    findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.Fragment4)
                }
            }
            true
        }

Fragments are all the same but pull different data from Firebase Database:

class Fragment : Fragment() {
    private var _binding: FragmentBinding? = null
    private val binding get() = _binding!!
    private lateinit var arrayList: ArrayList<FireItems>
 
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentBinding.inflate(inflater, container, false)
        val view = binding.root
   
        arrayList = ArrayList()

        val database = Firebase.database
        val myRef = database.getReference("items")
        myRef.addValueEventListener(object : ValueEventListener {
            @SuppressLint("NotifyDataSetChanged")
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                val value = dataSnapshot.value
                arrayList.clear()
                Log.d("mDatabase", "Value is: $value")
                for (postSnapshot in dataSnapshot.children) {

                    arrayList.add(FireItems(postSnapshot.child("image").value.toString(), postSnapshot.child("name").value.toString(), postSnapshot.child("description").value.toString()))
                    if (!arrayList.isNullOrEmpty()) {
                        binding.apply {
                            progressCircular.visibility = View.GONE
                            recyclerView.setHasFixedSize(true)
                            val adapter = FireRecyclerViewAdapter(requireActivity(), arrayList)
                            recyclerView.layoutManager = GridLayoutManager(requireActivity(), 2)
                            recyclerView.adapter = adapter
                        }
                    }
                }

            }

            override fun onCancelled(error: DatabaseError) {
                Log.w("mDatabase", "Failed to read value.", error.toException())
            }
        })

        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {}
        })

        return view
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}

I need to make it so that when I move from one fragment to another through the BottomNavigationView, fragments are not recreated each time. Because, firstly, if there are a lot of elements in the list, I don’t want to have to scroll through the entire recycler every time, and secondly, pulling the database every time is not correct as far as I know. Please tell me how to do it better?

0 Answers
Related