is it normal if my image from camera in ImageView disappear after segue to another fragment using navigation component?

若如初见. 提交于 2021-01-25 01:46:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!