问题
I try to capture an image using camera then the image result will be placed on my ImageView in a fragmentA like the code below, so it it NOT fetching an image path from Server then place it in ImageView using Glide Library, I take an image from camera.
val image_uri = Uri.fromFile(photoFile)
photoImageView.setImageURI(image_uri)
and then, I move to next destination using this code (from fragmentA to fragmentB)
val toFragmentB = AFragmentDirections.actionGlobalBFragment()
findNavController().navigate(toFragmentB)
but when I get back to FragmentA, the image in ImageView is disappear. I believe onDestroy
in FragmentA is not called when I segue to FragmentB. is this normal ?
so to solve this issue, to make the image persist, first I have to 'store' the image in ViewModel ? am I right?
回答1:
Check out the fragment lifecycle, especially view and fragment lifecycle flow. When you navigating out of a fragment, it's switching to CREATED
state (so onPause
and onStop
are called), but view is destroyed(and onDestroyView
is called). Next time you navigating back to this fragment, the view hierarchy is recreated (your onCreateView
is called). So you should set your image url to the image view again.
来源:https://stackoverflow.com/questions/65056767/is-it-normal-if-my-image-from-camera-in-imageview-disappear-after-segue-to-anoth