6

Is there an api to call methods from the Activity class? I need to call finish() on my app but couldn't find anything relevant in the docs.

To be more specific, I want to finish() the MainActivity from my index.android.jsx, when a particular TouchableHighlight is pressed.

[update]

For now I'm exposing a finish() method from my NativeModule but maybe there's a better way to do that.

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

4
  • You can call finish() in the activity class in order to destroy the activity. Is that what you're asking? Commented Oct 26, 2015 at 18:05
  • No, I want to call it from a React Native component on my index.android.jsx Commented Oct 26, 2015 at 18:10
  • did you find any solution for this @buenoFelipe i have same issue Commented Jan 17, 2017 at 16:53
  • 1
    FYI, if all you're trying to do is finish() the React Native Activity then you can import BackAndroid from react-native and then use BackAndroid.exitApp(). Commented Jul 15, 2017 at 19:36

1 Answer 1

2

I don't see a built in way to do this either and your approach using NativeModule is what I use as well.

However, if you call setCurrentActivity when building your ReactInstanceManager then you can call getCurrentActivity from your module instead of passing it through the ReactPackage and ReactModule constructors.

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

public class MyReactPackage implements 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 Setup

mReactInstanceManager = ReactInstanceManager.builder()
        .addPackage(new MyReactPackage())
        .setCurrentActivity(this)
        // other settings
        .build();
3
  • Hey, thank you, @mpkuth. Unfortunately (fortunately?), I don't use React Native anymore but I'm sure your answer will be useful for someone else. Commented Jul 1, 2016 at 19:32
  • I had to use a different name for the finish method Commented Nov 1, 2017 at 12:40
  • hello @mpkuth, can you help me find an idea how can I do the opposite, I mean, I want to open a react native component from the Native java code Commented Nov 19, 2017 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.