The module exposes four methods: setItem, getItem, removeItem, and clear. After changing the TypeScript spec, the host runs Codegen to regenerate the Android abstract class, which provides methods and annotations for the implementation.
Implementing an Android TurboModule from Scratch
An unidentified Callstack host demonstrates implementing an Android local storage TurboModule with SharedPreferences.
The host uses named SharedPreferences called Local Storage, which persist across app restarts in the app's private directory. The four methods store and retrieve strings, remove a key, or clear all entries. Kotlin's edit extension wraps the write operations and applies them asynchronously.
The host describes the SharedPreferences KTX operations as asynchronous and non-blocking. In the debugger, getItem runs on the JavaScript thread because it returns a value synchronously, while setItem runs on React Native's native module thread. The host advises using a promise-based method for long-running reads.
The host tests the module in a simulator with breakpoints and confirms that an alert displays “Hello Expo.” The recap highlights SharedPreferences as Android's counterpart to iOS UserDefaults, the edit extension, and the generated spec's TypeScript source.
Takeaways
- Regenerate Android Codegen files after changing the TypeScript module spec.
- Use SharedPreferences for persistent string key-value storage on Android.
- Use the edit extension to apply SharedPreferences writes, removals, and clears.
- Keep synchronous TurboModule reads short because they run on the JavaScript thread.
- Use a promise-based method when a read may take a long time.
Mentioned in this talk
Search inside this talk
What was claimed 5
- usesTurboModules uses TypeScript
“this is generated from the TypeScript interface”
YouTube Unverified1 source· Talk
- featureSharedPreferences: Persists across app restarts and is stored as an XML file in the app's private directory.
“They persist across app restarts. They're stored as an XML file in the app's private directory.”
YouTube Unverified1 source· Talk
- uses
“we need to do is add SharedPreferences”
YouTube Unverified1 source· Talk
- featureSharedPreferences: The KTX edit extension applies changes asynchronously and non-blockingly.
“All of these KTX extensions we've been using are asynchronous and non-blocking.”
YouTube Unverified1 source· Talk
- featureTurboModules: Synchronous getItem runs on the JavaScript thread; setItem runs on React Native's native module thread.
“getItem ... gets called on the JavaScript thread”
YouTube Unverified1 source· Talk










