Call methods from the Activity class (React Native Android)

Is there an api method to call from a class Activity? I need to call finish()in my application but cannot find anything in the docs.

To be more specific, I want finish() MainActivityfrom my index.android.jsx when a specific one is clicked TouchableHighlight.

[update]

I am currently exposing a method finish()from my NativeModule, but there may be a better way to do this.

https://github.com/sneerteam/react-native-sneer/blob/master/src/main/java/me/sneer/react/SneerModule.java#L84

+4
source share
1 answer

, NativeModule - , .

, setCurrentActivity ReactInstanceManager, getCurrentActivity ReactPackage ReactModule.

MyReactModule.java

public class MyReactModule extends ReactContextBaseJavaModule {

    public MyReactModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    @ReactMethod
    public void finish() {
        Activity activity = getCurrentActivity();
        if (activity != null) {
            activity.finish();
        }
    }

}

MyReactPackage.java

MyReactPackage ReactPackage {

    @Override
    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new MyReactModule(reactContext));
        return modules;
    }

}

ReactInstanceManager

mReactInstanceManager = ReactInstanceManager.builder()
        .addPackage(new MyReactPackage())
        .setCurrentActivity(this)
        // other settings
        .build();
+1

Source: https://habr.com/ru/post/1613240/


All Articles