1.10.15.11. fejezet, onDestroy event

android/app/src/main/[package]/MainActivity.java

...
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
import android.util.Log;
 
public class MainActivity extends ReactActivity {
  private static final String TAG = "MainActivity";
 
  @ReactMethod
  public void addListener(String eventName) {
 
  }
 
  @ReactMethod
  public void removeListeners(Integer count) {
 
  }
 
  @Override
  public void onDestroy() {
    super.onDestroy();
    ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
    WritableMap params = Arguments.createMap();
    params.putString("event", "onDestroy");
 
    if (reactContext != null) {
      getReactInstanceManager().getCurrentReactContext()
          .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
          .emit("ActivityStateChange", params);
 
    }
    Log.i(TAG, "onDestroy: ");
  }
...

App.tsx-be beregisztrálni az eventet:

import { DeviceEventEmitter } from 'react-native';
import React, { useEffect } from 'react';
 
const App = () => {
...
  useEffect(() => {
    let firebaseHelper: FirebaseHelper = new FirebaseHelper();
    DeviceEventEmitter.addListener('ActivityStateChange', e => {
      firebaseHelper.onDestroy();
      console.log('onDestroy');
    });
    firebaseHelper.geoQuerty(geofireCallback);
 
    return () => DeviceEventEmitter.removeAllListeners();
  }, []);
...
}

FirebaseHelper.ts

...
import { Database, DatabaseReference, getDatabase, goOffline, ref } from "firebase/database";
...
export default class FirebaseHelper {
...
 public onDestroy() {
        goOffline(this.db);
        firebase.app().delete();
    }
...
}