Is it possible to programmatically restart a React Native app without writing any native code?
For instance, I know from the answer to this question that I can restart an Android app with:
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Can I do the same with React Native?
If you want to restart just the JS part, then you can use React Native Restart Package. This will work on both Android and iOS.
If you want to restart the whole application, There are no react native packages as of now. If you want to create on your own, then check the following link
Building the custom Android module for React Native
If you find difficulty in writing the base JAVA code, You can generate the boilerplate using React Native Create Library
UPDATE:
From 0.62, React Native have added DevSettings.reload()
. So, if you want to reload programmatically, you don't need any third part packages from now on.
Click here for Doc
In addition to what's been said above, you can restart the app using Codepush like so:
import CodePush from 'react-native-code-push';
CodePush.restartApp();
In fact, that's where React Native Restart Package got its code from.
For iOS, React Native exposes a "reload" method via "DevSettings" https://github.com/facebook/react-native/blob/75f2da23c5d557862cf4b7bcdd8a1445b54d1c31/React/Modules/RCTDevSettings.mm#L240-L243
import { NativeModules } from "react-native";
NativeModules.DevSettings.reload();
You can use ReactInstanceManager like this
final ReactInstanceManager instanceManager = getReactInstanceManager();
instanceManager.recreateReactContextInBackground();
You can do as follow:
yarn add react-native-restart
react-native link react-native-restart
and use as follow:
import RNRestart from 'react-native-restart'; // Import package from node modules
// Immediately reload the React Native Bundle
RNRestart.Restart();
You don't need an external package for it.
DevSettings.reload()
is native from 62+. Here's the documentation for DevSettings module https://reactnative.dev/docs/devsettings#reload