diff --git a/android/src/main/java/com/reactnativedetector/DetectorModule.kt b/android/src/main/java/com/reactnativedetector/DetectorModule.kt index 189e51f..5c1b50e 100644 --- a/android/src/main/java/com/reactnativedetector/DetectorModule.kt +++ b/android/src/main/java/com/reactnativedetector/DetectorModule.kt @@ -4,29 +4,34 @@ import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.modules.core.DeviceEventManagerModule - - +import android.app.Activity class DetectorModule(val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), ScreenshotDetectionListener { - private val screenshotDetectionDelegate = ScreenshotDetectionDelegate(reactContext, this) + private var screenshotDetectionDelegate: ScreenshotDetectionDelegate? = null + override fun getName(): String { return "Detector" } @ReactMethod fun startScreenshotDetection() { - screenshotDetectionDelegate.startScreenshotDetection() + val currentActivity = reactContext.currentActivity + if (currentActivity != null) { + screenshotDetectionDelegate = ScreenshotDetectionDelegate(currentActivity, this) + screenshotDetectionDelegate?.startScreenshotDetection() + } } @ReactMethod fun stopScreenshotDetection() { - screenshotDetectionDelegate.stopScreenshotDetection() + screenshotDetectionDelegate?.stopScreenshotDetection() + screenshotDetectionDelegate = null; } override fun onScreenCaptured(path: String) { reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) - .emit("UIApplicationUserDidTakeScreenshotNotification", null) + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) + .emit("UIApplicationUserDidTakeScreenshotNotification", null) } override fun onScreenCapturedWithDeniedPermission() {