I implemented native module on React Native / Android Project. In android native project, I used startActivity function to move to the new activity I created manually. I will share some of the codes.
//MainApplication.java
public class MainApplication extends MultiDexApplication {
...
// Needed for `react-native link`
public List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new AnExampleReactPackage(this)
);
}
...
}
Here, if I use the code new MainReactPackage() then I see the error while running app on my android device.
Native module AccessibilityInfoModule tried to override AccessibilityInfoModule for module name AccessibilityInfo. If this was your intention, set canOverrideExistingModule=true
But I'm not sure how I can set the canOverrideExistingModule. How can I solve this?
Relative Question: React Native: Android activity go back
To set the canOverrideExistingModule prop when creating a native module in a React Native app, you can pass it as an argument to the ReactPackage constructor.
In your MainApplication.java file, you can update the getPackages method like this:
public class MainApplication extends MultiDexApplication { ... // Needed for `react-native link`public List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new AnExampleReactPackage(this, true) // Set canOverrideExistingModule to true ); } ... }
This will allow your native module to override any existing modules with the same name.
If you want to go back to the previous activity in an Android app created with React Native, you can use the BackHandler module and the exitApp method to exit the app, or use the startActivity method to start a new activity and specify the FLAG_ACTIVITY_CLEAR_TOP flag to clear the current activity and all others above it from the stack.
Here's an example of how you might use the BackHandler module and the exitApp method to go back to the previous activity:
import { BackHandler } from 'react-native'; const goBack = () => { BackHandler.exitApp(); }; // Call goBack when the back button is pressedBackHandler.addEventListener('hardwareBackPress', goBack); // Remove the event listener when the component is unmounteduseEffect(() => { return () => { BackHandler.removeEventListener('hardwareBackPress', goBack); }; }, []);