When ever a user shares some text from any other app, I am displaying my app in the Share sheet.The code is working fine as I have written Native
module for Android
. Below is the native code in Kotlin
. This use case is Android
specific and I have not implemented iOS
module yet
class ShareMenuModule(private val context: ReactApplicationContext): ReactContextBaseJavaModule(context), ActivityEventListener { override fun getName(): String { return "ShareMenuModule" } init { context.addActivityEventListener(this) } @ReactMethod fun getSharedText(successCallBack: Callback){ val intent = currentActivity?.intent val text = extractShared(intent) successCallBack.invoke(text) clearSharedText() } private fun extractShared(intent: Intent?): String?{ val action = intent?.action val type = intent?.type if(Intent.ACTION_SEND == action){ return if("text/plain" == type){ intent.getStringExtra(Intent.EXTRA_TEXT) } else {"" } } return "" } private fun clearSharedText(){ val intent = currentActivity?.intent val type = intent?.type if("text/plain" == type){ intent.removeExtra(Intent.EXTRA_TEXT) return } } private fun dispatchEvent(sharedText: String?){ if(!context.hasActiveCatalystInstance()){ return }// context.getJSIModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.javaPrimitiveType)// .e } override fun onNewIntent(intent: Intent?) { val text = extractShared(intent) dispatchEvent(text) currentActivity?.intent = intent } override fun onActivityResult(activity: Activity?, requestCode: Int, resultCode: Int, data: Intent?) { }}
To handle this in React Native I have written following code in the first screen of my app
const { ShareMenuModule } = NativeModules;
useEffect(()=>{ShareMenuModule.getSharedText(handleShareForAndroid);},[]);const handleShareForAndroid = useCallback(async (text) => { try { if (text !== null && text !== undefined && text.length > 0) { const link = text.substring(text.indexOf("http")).trim(); navigation.reset({ index: 0, routes: [{ name: ADD_ITEM_SHARE_MENU_SCREEN, params: { link } }], }); } else { navigate(MAIN_SCREEN); } } catch (e) { navigate(MAIN_SCREEN); } });
Now when my app is completely closed , the above code works fine when I try to share some url or text from some other app. My app is displayed in share sheet and I am able to handle the url.But suppose my app is in foreground,and the user is on some other screen which is not the starting screen of the app and the user puts it in background and goes to some other app and share text even though my app is displayed in share sheet but as the user was not on the starting screen I am not able to handle the shared url or text. So do I need to write the code which I have written on the starting screen of my React Native app to every other screen. As I have a lot of screens in my app it is not feasible to write in every other screen. Is there a better approach. I tried writing the handleShare code in App.js but it is not detected when I am on other screen. There is concept of Deep Linking
in react navigation
but I don't think that is what I should be looking into.