Is it possible to remember a value (`Bitmap`) for ...
# compose
z
Is it possible to remember a value (
Bitmap
) for longer than the usual
remember
, but without using
rememberSaveable
? Im basically putting my screen in the backstack for a few moments, and upon return Id still like to be able to access the bitmap! At the same time, I dont want to cache it in a variable outside of composition 💀
This question can also be rephrased to: How can I know when my composable is actually 100% leaving the composiiton without being part of the backstack?
a
The typical solution would be a
ViewModel
but you could also create your own
remember
which stores the bitmap in the
nonconfigurationinstance
May I ask why you dont want to store cache the bitmap outside of the composition?
z
Can you please elaborate on what you mean by
remember
that stores it in
nonconfigurationinstance
? About storing it outside of composition -> I would have no way of knowing when to dispose it
Also, not using viewmodels in my project, otherwise I think its the best or at least simplest solution to this!
p
To know when your Composable is leaving the composition 100%, doesn't DisposableEffect work for this matter? I would place a disposable effect in the screen you said will be put in the backstack for a moment, then place some logs to see what happens when it is place in the backstack and presented again
z
The DisposableEffect.onDispose is called when the composable enters the backstack unfortunately!
And that makes sense, its no longer called while its in the backstack 😃
p
Got you, unfortunately for your use case
z
Ultimately I got it working with
rememberSaveable
(I forgot that Bitmap was Parcelable). Not sure if its the best solution, but Im glad the Bitmap goes away when the composable does. If anyone has any better solution still, Id love to hear it!
👍 1
y
Do you have any sort of scope similar to ActivityRetained or ViewModel in your app? Or you are just using rememberSaveable to restore?
z
Yes, its effectively similar to how viewmodels are scoped to navgraph destinations. BUT, this blur effect is a design system component.
a
Saving the bitmap is usually not the best solution, because the bitmap can easily exceed 1mb which is the limit for parcels of modern devices
☝️ 1
But your comment gave me a nice idea for a good solution for you: instead of saving the bitmap to a parcel, you can create a remembersaveable that stores the bitmap in a cache file and saves the uri of that file to the parcel
In that way you will avoid the parcel size limitation because only the cache file path will be contained inside of the parcel
z
When/how would I remove the bitmap from the cache in that case?
a
That’s an interesting question, let me check the api if there is something suitable
🙏🏽 1
y
Where does the bitmap come from originally? an Image Loader library would handle this normally. And take in a URL.
z
I'm creating it from my composable, similar to taking a screenshot of it!
Basically doing that and applying a blur to it, then rendering it. So it looks weird if it disappears momentarily when you navigate back to this screen.
Unfortunately I have not been able to find a solution to this yet! Feel free to suggest if you have any ideas 😃
rememberSaveable
is the closest to a proper solution, but as others have mentioned; it will likely cause issues due to the size of the bitmaps.
I dont think this is possible to do with compose only 😞 I ended up involving my framework .. so basically caching the bitmap in a viewmodel of sorts. It works as expected, the bitmap is removed when the viewmodel leaves the backstack, etc. Its an okay workaround for now, but ultimately I want to find a way to do this without involving the framework. If anyone has any further ideas, please let me know 🙏🏽 Thanks for the discussions!
101 Views