1

using fragment navigation, when I move temp1 to temp2 and temp2 to temp1 again, I see the fragment is generated newly. But when I use fragmentManager.replace, it reuse the fragment that I already used.

resuing code

    private lateinit var binding : ActivityMainBinding
    private  var currentFragment: Fragment?=null
    private  var homeFragment = HomeFragment()
    private  var temp1Fragment =Temp1Fragment()
    private  var temp2Fragment= Temp2Fragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initBottomNavItemClick()
        currentFragment = homeFragment
    }


    private fun initBottomNavItemClick(){
        binding.bottomNavigationView.setOnNavigationItemSelectedListener {
            when(it.itemId){
                R.id.homeFragment->{
                //    findNavController(R.id.navFragment).navigate(R.id.homeNavFragment)
                    currentFragment = homeFragment
                    changeFragment(this,binding.fragmentContainer,currentFragment!!)
                }
                R.id.tempFragment1->{
                   // findNavController(R.id.navFragment).navigate(R.id.temp1NavFragment)
                    currentFragment = temp1Fragment
                    changeFragment(this,binding.fragmentContainer,currentFragment!!)
                }
                R.id.tempFragment2->{
                   // findNavController(R.id.navFragment).navigate(R.id.temp2NavFragment)
                    currentFragment = temp2Fragment
                    changeFragment(this,binding.fragmentContainer,currentFragment!!)
                }
            }

            true
        }
    }

So when I use fragmentManager, I can preserve the recyclerview position when I come back, but not fragment navigation. fragment navigation generates a new fragment and the position start at 0. is fragment navigation possible to keep the previous status not generating newly?

Charles
  • 181
  • 1
  • 14
  • with only NavigationComponent it will not be possible [more info here](https://github.com/android/architecture-components-samples/issues/530), use [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel) instead, – Nikhil Sharma Aug 05 '20 at 16:38

1 Answers1

0

I will answer your question in two part.

  • Solution
  • Explanation

Solution

Do not inflate view every time you are coming back to previous fragment. Save View in a local variable and inflate it only once. Suggested by Ian Lanke

private var savedViewInstance: View? = null

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    return if (savedViewInstance != null) {
        savedViewInstance
    } else {
        savedViewInstance =
                inflater.inflate(R.layout.fragment_professional_details, container, false)
        savedViewInstance
    }
}

Explanation

Let's understand life cycle of a fragment under navigation architecture.

Scenario: We are taking two fragments, HomeFragment and DashboardFragment. Both fragments belong to same NavGraph and start destination is Home Fragment.

Fragment Life Cycle on launching of app-

HomeFragment: onAttach:

HomeFragment: onCreate:

HomeFragment: onCreateView:

HomeFragment: onViewCreated:

HomeFragment: onActivityCreated:

HomeFragment: onStart:

HomeFragment: onResume:

On Navigation: Home Fragment ---> Dashboard Fragment

DashboardFragment: onAttach:

DashboardFragment: onCreate:

DashboardFragment: onCreateView:

DashboardFragment: onViewCreated:

DashboardFragment: onActivityCreated:

DashboardFragment: onStart:

DashboardFragment: onResume:

HomeFragment: onPause:

HomeFragment: onStop:

HomeFragment: onDestroyView:

On Navigation: Dashboard Fragment ---> Home Fragment

HomeFragment: onAttach:

HomeFragment: onCreate:

HomeFragment: onCreateView:

HomeFragment: onViewCreated:

HomeFragment: onActivityCreated:

HomeFragment: onStart:

HomeFragment: onResume:

DashboardFragment: onPause:

DashboardFragment: onStop:

HomeFragment: onDestroy:

DashboardFragment: onDestroyView:

DashboardFragment: onDestroy:

If we are saving view on intial HomeFragment: onCreateView() and inflating same view every time for next call of HomeFragment: onCreateView(), we can get old view restored.

If you notice HomeFragment: onDestroy() will be called but after HomeFragment: onViewCreated() has called. Calling of HomeFragment: onDestroy() is just destroying old instance of HomeFragment.

I still believe this way of doing things are not best practice but it will be until, Google will come up something like onFragemntRestore().

To actually update the view, you must need to ViewModel and observe the changes to change in the views.

For more information you go can go through this thread.
happy coding !

Nikhil Sharma
  • 897
  • 1
  • 4
  • 18