Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3365823
D5933.id19560.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D5933.id19560.diff
View Options
diff --git a/native/expo-modules/android-lifecycle/android/build.gradle b/native/expo-modules/android-lifecycle/android/build.gradle
--- a/native/expo-modules/android-lifecycle/android/build.gradle
+++ b/native/expo-modules/android-lifecycle/android/build.gradle
@@ -88,4 +88,9 @@
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
+
+ implementation "androidx.lifecycle:lifecycle-runtime:2.5.1"
+ implementation "androidx.lifecycle:lifecycle-process:2.5.1"
+
+ implementation 'com.facebook.react:react-native:+'
}
diff --git a/native/expo-modules/android-lifecycle/android/src/main/java/app/comm/android/lifecycle/AndroidLifecycleModule.kt b/native/expo-modules/android-lifecycle/android/src/main/java/app/comm/android/lifecycle/AndroidLifecycleModule.kt
--- a/native/expo-modules/android-lifecycle/android/src/main/java/app/comm/android/lifecycle/AndroidLifecycleModule.kt
+++ b/native/expo-modules/android-lifecycle/android/src/main/java/app/comm/android/lifecycle/AndroidLifecycleModule.kt
@@ -1,38 +1,66 @@
package app.comm.android.lifecycle
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleObserver
+import androidx.lifecycle.OnLifecycleEvent
+import androidx.lifecycle.ProcessLifecycleOwner
+import com.facebook.react.bridge.UiThreadUtil
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class AndroidLifecycleModule : Module() {
- // Each module class must implement the definition function. The definition consists of components
- // that describes the module's functionality and behavior.
- // See https://docs.expo.dev/modules/module-api for more details about available components.
+ private val lifecycleStates = mapOf(
+ "ACTIVE" to "active",
+ "BACKGROUND" to "background",
+ )
+ private val eventName = "LIFECYCLE_CHANGE"
+ private val statusKey = "status"
+
+ private val lifecycle: Lifecycle
+ get() = ProcessLifecycleOwner.get().getLifecycle()
+
+ private val observer = object : LifecycleObserver {
+ @OnLifecycleEvent(Lifecycle.Event.ON_START)
+ fun onStart() {
+ sendEvent(
+ eventName,
+ mapOf(statusKey to requireNotNull(lifecycleStates["ACTIVE"]))
+ )
+ }
+
+ @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
+ fun onStop() {
+ sendEvent(
+ eventName,
+ mapOf(statusKey to requireNotNull(lifecycleStates["BACKGROUND"]))
+ )
+ }
+ }
+
override fun definition() = ModuleDefinition {
- // Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
- // Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
- // The module will be accessible from `requireNativeModule('AndroidLifecycle')` in JavaScript.
Name("AndroidLifecycle")
- // Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary.
- Constants(
- "PI" to Math.PI
- )
+ Constants({
+ val currentState =
+ if (lifecycle.getCurrentState() == Lifecycle.State.RESUMED)
+ requireNotNull(lifecycleStates["ACTIVE"])
+ else
+ requireNotNull(lifecycleStates["BACKGROUND"])
+ return@Constants lifecycleStates + ("initialStatus" to currentState)
+ })
- // Defines event names that the module can send to JavaScript.
- Events("onChange")
+ Events(eventName)
- // Defines a JavaScript synchronous function that runs the native code on the JavaScript thread.
- Function("hello") {
- "Hello world! 👋"
- }
+ OnStartObserving({
+ UiThreadUtil.runOnUiThread({
+ lifecycle.addObserver(observer)
+ })
+ })
- // Defines a JavaScript function that always returns a Promise and whose native code
- // is by default dispatched on the different thread than the JavaScript runtime runs on.
- AsyncFunction("setValueAsync") { value: String ->
- // Send an event to JavaScript.
- sendEvent("onChange", mapOf(
- "value" to value
- ))
- }
+ OnStopObserving({
+ UiThreadUtil.runOnUiThread({
+ lifecycle.removeObserver(observer)
+ })
+ })
}
}
diff --git a/native/lifecycle/lifecycle-module.js b/native/lifecycle/lifecycle-module.js
--- a/native/lifecycle/lifecycle-module.js
+++ b/native/lifecycle/lifecycle-module.js
@@ -5,32 +5,42 @@
NativeModulesProxy,
EventEmitter,
} from 'expo-modules-core';
+import invariant from 'invariant';
+import { Platform } from 'react-native';
import type { EmitterSubscription } from '../types/react-native';
-const AndroidLifecycleModule: {
- +PI: number,
- +hello: () => string,
- +setValueAsync: string => Promise<void>,
- ...
-} = requireNativeModule('AndroidLifecycle');
-
-export const PI = AndroidLifecycleModule.PI;
+type Active = 'active';
+type Background = 'background';
+type LifecycleStatus = Active | Background;
-export function hello(): string {
- return AndroidLifecycleModule.hello();
-}
-
-export async function setValueAsync(value: string): Promise<void> {
- return await AndroidLifecycleModule.setValueAsync(value);
+let AndroidLifecycleModule: ?{
+ +ACTIVE: Active,
+ +BACKGROUND: Background,
+ +initialStatus: LifecycleStatus,
+ ...
+};
+let emitter;
+if (Platform.OS === 'android') {
+ AndroidLifecycleModule = requireNativeModule('AndroidLifecycle');
+ emitter = new EventEmitter(
+ AndroidLifecycleModule ?? NativeModulesProxy.AndroidLifecycle,
+ );
}
-const emitter = new EventEmitter(
- AndroidLifecycleModule ?? NativeModulesProxy.AndroidLifecycle,
-);
+export const ACTIVE: ?Active = AndroidLifecycleModule?.ACTIVE;
+export const BACKGROUND: ?Background = AndroidLifecycleModule?.BACKGROUND;
+export const initialStatus: ?LifecycleStatus =
+ AndroidLifecycleModule?.initialStatus;
-export function addChangeListener(
- listener: ({ +value: string }) => mixed,
+export function addAndroidLifecycleListener(
+ listener: (state: LifecycleStatus) => mixed,
): EmitterSubscription {
- return emitter.addListener('onChange', listener);
+ invariant(
+ Platform.OS === 'android' && emitter,
+ 'Only Android should call addAndroidLifecycleListener',
+ );
+ return emitter.addListener('LIFECYCLE_CHANGE', ({ status }) =>
+ listener(status),
+ );
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 26, 7:52 AM (15 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2583413
Default Alt Text
D5933.id19560.diff (6 KB)
Attached To
Mode
D5933: [native] [32/40] RN 0.70: Working AndroidLifecycle Expo module
Attached
Detach File
Event Timeline
Log In to Comment