X: I know what is `remember`, but what is `rememberSaveable`? How are they different? 🤷🏼

Y: Well, as you know, `remember` is to survive your value during recomposition...

#compose #thread_ahead 🧵 ⬇️
...`rememberSaveable` is to survive the value during both recomposition and activity recreation

X: Ahm...Okay, how do I use it? 🤔
Y: Let's look at the method's signature first.👀 Image
X: What is that `inputs`? and Why is it `vararg`?🤷🏼

Y: `inputs` are something that we give to `rememberSaveable` to tell something like, "Hey, if any of the inputs are changed during recomposition, ...
... I want you to invalidate the old value (if exists), and create a new instance of the object by calling the `init` block I passed"
X: Okay, that's nice. Tell me about the `saver`? 🤨

Y: By default, `rememberSaveable`, can only save values that can live inside a Bundle. Such as Int, String, Parcelable, Serializable, etc. It doesn't know how to serialize/deserialize your "custom" object.
So, to do that, we need to teach how it can save/restore a custom object/class. That's the purpose of Saver. It provides two callbacks. One to save the object, and another one to restore the object.

Very similar to Room's @TypeConverter, but different syntax. Image
X: Cool. Is there any real-world example I can look into? 😮
Y: Sure. The `navigation-compose` library uses this technique to restore the `navController`'s state during activity recreation.

Source: cs.android.com/androidx/platf… Image
NOTE: If you think any of the above points are incorrect, please correct me. Also, you're free to hijack this thread if I missed anything about `rememberSaveable` 😉

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with theapache64  🚁⛰️

theapache64  🚁⛰️ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(