Wie sende ich ein Ereignis von MainActivity an React Native in einer neuen Architektur (Bridgeless-Modus)?Android

Forum für diejenigen, die für Android programmieren
Anonymous
 Wie sende ich ein Ereignis von MainActivity an React Native in einer neuen Architektur (Bridgeless-Modus)?

Post by Anonymous »

Ich habe die folgende Implementierung (ich habe Importe und einige Teile übersprungen):

Code: Select all

class MainActivity : ReactActivity(), DefaultHardwareBackBtnHandler, PermissionAwareActivity {
var communicationModule: CommunicationModule? = null

fun sendMessageToNative(message: String) {
val manager = (application as MyApplication).reactNativeHost.reactInstanceManager
val reactContext = manager.currentReactContext

if (reactContext != null) {
val module = reactContext?.getNativeModule(CommunicationModule::class.java)
if (module != null) {
module.emitOnPush(message)
}
} else {
manager.addReactInstanceEventListener(object :
ReactInstanceManager.ReactInstanceEventListener {
override fun onReactContextInitialized(context: ReactContext) {
val module = context.getNativeModule(CommunicationNewModule::class.java)
if (module != null) {
module.emitOnPush(message)
}
}
})
manager.createReactContextInBackground()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(null)
// ...
}

override fun onBackPressed() {
super.onBackPressed()
sendMessageToNative("MY_MESSAGE") // this doesn't work (no event on RN side)
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
// assume this is not empty
val myMessage = intent?.extras["message"]
sendMessageToNative("MY_MESSAGE") // this doesn't work (no event on RN side)
}
}
und Turbo-Modul (

Code: Select all

NativeCommunication.ts
)

Code: Select all

import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes';

export interface Spec extends TurboModule {
onPush: EventEmitter;
}

export default TurboModuleRegistry.getEnforcing(
'NativeCommunication',
);
und Kotlins (

Code: Select all

CommunicationModule.kt
)

Code: Select all

@ReactModule(name = CommunicationModule.NAME)
class CommunicationModule(private val reactContext: ReactApplicationContext) : NativeCommunicationSpec(reactContext) {

override fun getName() = NAME

companion object {
const val NAME = "NativeCommunication"
}
}
und schließlich Listener auf der React Native-Seite festlegen (z. B. auf einer Seite oder MyPage.tsx)

Code: Select all

const MyPage = ({ navigation }: MyPageProps): JSX.Element => {
const listenerSubscription = useRef(null);
const callbackMethod = (message: string) => { console.log(message) }

useEffect(() => {
listenerSubscription.current = NativeCommunication?.onPush(callbackMethod);
return () => {
listenerSubscription.current?.remove();
listenerSubscription.current = null;
}
}, []);
}
PROBLEM: Ereignisse, die in onNewIntent und onBackPressed ausgelöst werden, kommen nicht bei React Native an.
Meine Implementierung funktioniert (Ereignisse kommen bei React Native an) nur, wenn sie von
ausgelöst wird

Code: Select all

fun MainActivity.newSessionTimer(): CountDownTimer {
val sessionTimerFinishAfter = inMilliseconds(30, TimeUnit.Second)
val sessionTimerTickEvery = inMilliseconds(30, TimeUnit.Second)
return object : CountDownTimer(sessionTimerFinishAfter, sessionTimerTickEvery) {
override fun onTick(millisUntilFinished: Long) { /** no-op */ }
override fun onFinish() { sendMessageToNative("MY_MESSAGE") }
}
}
ZIEL: Nachrichten von onNewIntent und onBackPressed senden und auf MyPage.tsx empfangen können

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post