使用导航组件将我的图像从ImageView中的相机缝到另一个片段

使用导航组件将我的图像从ImageView中的相机缝到另一个片段

本文介绍了如果使用导航组件将我的图像从ImageView中的相机缝到另一个片段后消失了,这是正常的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用相机捕获图像,然后图像结果将以片段A的形式放置在我的ImageView上,如下代码所示,因此它从服务器获取图像路径,然后将其放置在使用Glide Library的ImageView,我从相机拍摄了图像.

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)

然后,我使用此代码(从fragmentA到fragmentB)移至下一个目的地

and then, I move to next destination using this code (from fragmentA to fragmentB)

val toFragmentB = AFragmentDirections.actionGlobalBFragment()
findNavController().navigate(toFragmentB)

但是当我回到FragmentA时,ImageView中的图像消失了.我相信当我选择FragmentB时,不会调用FragmentA中的 onDestroy .这正常吗?

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 ?

因此要解决此问题,使图像持久存在,首先我必须将图像存储"在ViewModel中吗?我说的对吗?

so to solve this issue, to make the image persist, first I have to 'store' the image in ViewModel ? am I right?

推荐答案

查看片段生命周期,尤其是视图和片段生命周期流.当您浏览片段时,它会切换到 CREATED 状态(因此,调用 onPause onStop ),但是 view被破坏了(并称为 onDestroyView ).下次您导航回到该片段时,将重新创建视图层次结构(调用 onCreateView ).因此,您应该将图像网址再次设置为图像视图.

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.

这篇关于如果使用导航组件将我的图像从ImageView中的相机缝到另一个片段后消失了,这是正常的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:16