Search code examples
androidpicassoandroid-lifecycleandroid-glide

Is it safe to use View.setTag as a way to record an image's hash?


I plan on using View.setTag(int, Object) to store the hashcode of a Uri object before loading it into an AppCompatImageView via Picasso or Glide.

I was wondering, during the android lifecycle, will there ever be a time when the AppCompatImageView gets destroyed and recreated - retaining the information from setTag but not the actual image itself - thereby creating inconsistency.

Am I guaranteed that as long as setTag exists, the loaded image will also exist?


Solution

  • I plan on using View.setTag(int, Object) to store the hashcode of a Uri object before loading it into an AppCompatImageView via Picasso or Glide.

    Back in the days of ListView optimization we use View.setTag(int, Object) to store information of that particular view, to be reused whenever it gets redrawn mind the word redrawn not recreated. So we can say untill object of the view is preserved or in other words View is not destroyed, it is safe.

    I was wondering, during the android lifecycle, will there ever be a time when the AppCompatImageView gets destroyed and recreated - retaining the information from setTag but not the actual image itself - thereby creating inconsistency.

    In a better this should not happen. Consider case of device rotation, a new View will be created and painted, so it won't be holding information of last tag being set.

    Am I guaranteed that as long as setTag exists, the loaded image will also exist?

    Let's say it like this, imageView.setTag(int, Object) will exist untill Your ImageView exist, so composition is from ImageView to Tag not vice-versa, in simple Java terms tag is just a class level property of View class, which ImageView or AppCompatImageView extends somewhere in the hierarchy. So if ImageView exist, the same ImageView object then your same tag will exist.