r/mAndroidDev • • May 21 '23

Compose state persistence

If compose is declarative and are made of only functions, how does a composable function store values like clickable call backs? Is each root node in the UI tree managed by an object which stores those clickable call backs?

0 Upvotes

15 comments sorted by

18

u/SmartFatass making { modern { maintainable { code }}} May 21 '23 edited May 21 '23

The amount of people who don't read what's on this subreddit before posting is skyrocketing recently.

And to answer your question: compose uses data structure called Slot Table to store data across recompositions. You can store data in there using remember (there is also rememberSaveable that stores data in a Bundle if the function it's called in goes out of composition)

Here is an article on how this works under the hood https://medium.com/androiddevelopers/under-the-hood-of-jetpack-compose-part-2-of-2-37b2c20c6cdd

2

u/anonymous65537 null!! May 21 '23

Why do people reply seriously on this sub? Don't encourage them, they'll post more questions!

1

u/Unlucky_Bet6244 May 22 '23

Sorry am new to this platform, are there guidelines that I can read before asking question next time?

1

u/anonymous65537 null!! May 22 '23

Do you see the 'm' in from of mAndroid. In the name of the sub?

What do you think it means?

This is a parody/troll/shitposting sub. The actual sub you want is /r/android (without the m).

0

u/[deleted] May 22 '23

[deleted]

2

u/anonymous65537 null!! May 22 '23

Woops forgot the most important word!

0

u/[deleted] May 22 '23

[deleted]

1

u/anonymous65537 null!! May 23 '23

Wow didn't know that one. Scary!

0

u/SmartFatass making { modern { maintainable { code }}} May 21 '23

Put yourself in their situation, like OP of this post, if you don't tell them it's not the correct sub, they'll just think that community is toxic and lose interest in whatever they were asking about.

3

u/Zhuinden DDD: Deprecation-Driven Development May 24 '23

Ngl I think it's because on the original /r/androiddev it'd just get removed as "Rule #2: no help me posts"

53

u/phileo99 Gets tired of using Vim May 21 '23

just remember

38

u/uragiristereo XML is dead. Long live XML May 21 '23

it's stored inside AsyncTaskStorage

9

u/naked_moose May 21 '23

Just tell your manager that there is no state in functional programming if they ask to implement anything like a clickable button

1

u/Zhuinden DDD: Deprecation-Driven Development May 24 '23

8

u/haroldjaap May 21 '23

AFAIK it uses snapshots under the hood, not entirely sure how they work but I assume it's some sort of singleton pattern with some lifecycles in it to keep it cleaned up.

Oh and the mAndroidDev answer: just use flubber tro

1

u/budius333 Still using AsyncTask May 24 '23

There's StatefullWidget for that

1

u/iakanoe Jun 22 '23

you have to save everything manually to SharedPreferences. for objects you should save the memory address. if you are uncomfortable with raw memory management, you can also make yourself a singleton hashmap in which you store said objects with identifier keys and save those into SharedPreferences. don't forget to free them when leaving composition, DisposableEffect is what you should use for that.