Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Architecture Fragment Reload Problem

I am using Navigation Architecture in an image gallery, when I go from fragment A to B and then return back to A, these 3 methods are called again which will cause my gallery to reload, where I should load my data in fragment so when I come back from B to A my methods don't get called? :

  1. OnCreateView
  2. OnViewCreated
  3. OnResume

Step A to B

like image 697
AVEbrahimi Avatar asked Jan 21 '19 09:01

AVEbrahimi


1 Answers

The trick is to not inflate the view again in onCreateView(). This will call all your lifecycle events again, but this is how you will be getting your fragment's state maintained.

This was suggested by Ian Lake from google's android team. Here is the reference.

var binding: FragmentFeedsBinding? = null

   override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    if (binding == null)
        binding = FragmentFeedsBinding.inflate(inflater, container, false)
    return binding?.root
}
like image 163
Daniyal Javaid Avatar answered Sep 28 '22 20:09

Daniyal Javaid