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? :
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With