Programmatically Restart a React Native App

AndroidIosReact Native

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 while developing you can use that. Note that this method does not work in production!

Click here for Doc

Solution 2 - Android

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.

Solution 3 - Android

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();

Solution 4 - Android

You can use ReactInstanceManager like this

final ReactInstanceManager instanceManager = getReactInstanceManager();
instanceManager.recreateReactContextInBackground();

Solution 5 - Android

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();

Solution 6 - Android

If you are using Expo you can also use https://docs.expo.io/versions/v38.0.0/sdk/updates/ specifically Updates.reloadAsync() as an alternative. The Updates API from expo allows you to programatically control and respond to over-the-air updates to your app.

Install

expo install expo-updates

Use

import * as Updates from 'expo-updates';

...

async function reloadApp () {
    await Updates.reloadAsync();
}

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam JakielaView Question on Stackoverflow
Solution 1 - AndroidSriramanView Answer on Stackoverflow
Solution 2 - AndroidMike MartinView Answer on Stackoverflow
Solution 3 - AndroidEstevão LucasView Answer on Stackoverflow
Solution 4 - AndroidMohit GoelView Answer on Stackoverflow
Solution 5 - Androidsajad abbasiView Answer on Stackoverflow
Solution 6 - AndroidJo E.View Answer on Stackoverflow
Solution 7 - AndroidEstevão LucasView Answer on Stackoverflow