@@ -43,24 +43,41 @@ SettingItem(title = "Dark Mode") {
4343}
4444```
4545
46- ### ** Function Signature**
46+ ### ** Function Signature and Explanation**
47+
48+ The ` rememberPreferenceStateOf ` function allows you to store and retrieve values from SharedPreferences while keeping them in sync with your Jetpack Compose UI. You can use this function in two different ways:
49+
50+ #### 1️⃣ Using a Key
51+
52+ This method is useful for primitive types like ` String ` , ` Int ` , ` Boolean ` , etc.
4753
4854``` kotlin
49- rememberPreferenceStateOf(
50- key: String ,
51- defaultValue: T ,
52- sharedPreferences: SharedPreferences = LocalSharedPreferences .current ? : defaultPreferences(),
53- saver: PreferenceSaver <T >? = null ,
54- vararg keys: Any
55+ var username by rememberPreferenceStateOf(
56+ key = " username" ,
57+ defaultValue = " Guest"
5558)
5659```
5760
58- ### ** Parameter Explanation**
5961- ` key: String ` → The key used to store the value in SharedPreferences.
6062- ` defaultValue: T ` → The default value returned if the key does not exist.
61- - ` sharedPreferences: SharedPreferences ` → The SharedPreferences instance to use (default: a SharedPreferences instance named "screen").
62- - ` saver: PreferenceSaver<T>? ` → Custom saver for complex types (optional).
63- - ` keys: Any ` → Additional keys to trigger recomposition when changed.
63+ - ` sharedPreferences: SharedPreferences ` → (Optional) A custom SharedPreferences instance.
64+ - ` keys: Any ` → (Optional) Additional keys to trigger recomposition when changed.
65+
66+ #### 2️⃣ Using a Custom Saver
67+
68+ This method is used for complex data types that require custom serialization.
69+
70+ ``` kotlin
71+ var user by rememberPreferenceStateOf(
72+ defaultValue = User (" " , " " ),
73+ saver = UserPreferenceSaver
74+ )
75+ ```
76+
77+ - ` saver: PreferenceSaver<T>? ` → A custom saver for serializing and deserializing objects.
78+ - ` defaultValue: T ` → The default value to use when no data is found.
79+ - ` sharedPreferences: SharedPreferences ` → (Optional) A custom SharedPreferences instance.
80+ - ` keys: Any ` → (Optional) Additional keys to trigger recomposition.
6481
6582> 🚨 ** Warning**
6683> You must provide either ` key ` or ` saver ` . If both are ` null ` , an exception will be thrown.
@@ -137,7 +154,6 @@ OutlinedTextField(
137154
138155This approach allows you to persist and manage complex data types seamlessly within your Jetpack Compose application.
139156
140-
141157---
142158
143159## 🔧 Customizing SharedPreferences
0 commit comments