new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Expo & React Native · all subjects

modules & native code

12 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Expo Modules API for adding native modules

The Expo Modules API is used to add and use native modules in your app.

Package interface for lifecycle listeners

Create a concrete class implementing the Package interface to hook into Android lifecycle events. Implement either createReactActivityLifecycleListeners or createApplicationLifecycleListeners methods depending on whether you need Activity or Application lifecycle hooks.

Expo Autolinking discovers Package classes automatically

Expo Autolinking scans each linked module's Android sources for classes in files ending in Package.java or Package.kt that implement the Package interface. During Android build, these classes are added to the generated ExpoModulesPackageList. You do not need to register the Package class in MainApplication.java, MainApplication.kt, or expo-module.config.json if the module already supports Android.

ReactActivityLifecycleListener supported callbacks

ReactActivityLifecycleListener hooks into React Native's ReactActivity lifecycle and supports the following Activity lifecycle callbacks: onCreate, onResume, onPause, onDestroy, onNewIntent, and onBackPressed.

Override only needed Activity lifecycle methods

When implementing ReactActivityLifecycleListener, you can override only the lifecycle methods you need. The interface uses Java 8 default methods, so you don't have to implement all methods. This reduces maintenance costs between Expo SDK versions.

Communicating from Activity lifecycle listener to JavaScript

Lifecycle listeners are singleton classes that exist independently of module instances. To communicate between a lifecycle listener and your module or JavaScript code, use the observer pattern: lifecycle listeners capture system events and notify module observers, which then send structured events to JavaScript via sendEvent. Use weak references to prevent memory leaks.

ApplicationLifecycleListener supported callbacks

ApplicationLifecycleListener supports the following Application lifecycle callbacks: onCreate and onConfigurationChanged.

Create ApplicationLifecycleListener in Package class

Implement createApplicationLifecycleListeners in your Package class, returning a list of ApplicationLifecycleListener instances. The method receives a context parameter of type Context.

Activity lifecycle listener example with deep links

Example of DeepLinkHandlerActivityLifecycleListener implementing onCreate and onNewIntent to capture deep links: onCreate calls handleIntent with activity.getIntent(), onNewIntent calls handleIntent with the received intent. handleIntent extracts the URI from intent.getData() and notifies observers.

Module with event sending and observer management

A module can maintain a static set of observer callbacks (e.g., urlReceivedObservers) that lifecycle listeners notify. The module should implement OnStartObserving and OnStopObserving to add and remove observers. Use WeakReference to store module references in observers to prevent memory leaks. sendEvent is called with event name and Bundle containing event data.

Why onStart and onStop Activity listeners are not supported

Lifecycle hooks are set up from ReactActivityDelegate rather than MainActivity. ReactActivityDelegate does not have onStart and onStop callbacks, so these are not yet supported in the Expo Modules API.

Interface stability and backward compatibility strategy

Listener interfaces may change between Expo SDK releases. The strategy for backward compatibility is to add new interfaces and use @Deprecated annotation for interfaces planned for removal. All interfaces are based on Java 8 interface default methods, so you don't have to implement all methods.

Give your agent this brain