10

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?

Binary
  • 421
  • 4
  • 12
  • 2
    Since Navigation 2.4.0, Navigation will keep the state of each item when you reselect that tab, but you are specifically throwing out that behavior by setting your own `setOnNavigationItemSelectedListener` that calls `navigate()` to go to a brand new instance. Why are you doing that at all? – ianhanniballake Feb 11 '22 at 19:31
  • 3
    @ianhanniballake Even if I create a project through google template with `val navController = findNavController(R.id.nav_host_fragment_activity_main) val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController)` fragments still recreted( – Binary Feb 12 '22 at 09:29
  • 1
    @ianhanniballake I am getting issue like him ? I have checked, the fragment is being destroyed (not just destroyedView). And then back to bottom item, the new fragment is created – Thân Hoàng May 10 '22 at 10:48
  • @ThânHoàng Did you find any solution for this? – Jarin Rocks Jul 25 '22 at 07:09
  • @JarinRocks Unfortunately no – Binary Jul 25 '22 at 19:04
  • try this https://blog.mindorks.com/saving-fragment-states-with-bottomnvaigationview @Binary – Rahul Singh Chandrabhan Oct 23 '22 at 16:53
  • Didn't you find any solution still? – ImC0der Dec 23 '22 at 20:00

0 Answers0