63

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?

Community
  • 1
  • 1
Adam Jakiela
  • 2,188
  • 7
  • 30
  • 48

7 Answers7

61

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

Falaen
  • 363
  • 4
  • 13
Sriraman
  • 7,658
  • 4
  • 40
  • 60
  • 2
    If you are/can consider using Expo you can achieve this with Util.reload(). https://docs.expo.io/versions/v15.0.0/sdk/util.html – sinewave440hz Aug 29 '17 at 16:28
  • 2
    The expo reload method has changed to `Expo.Updates.reload()` – lsimonetti May 14 '19 at 16:34
  • 5
    You don't need this package anymore. `DevSettings.reload()` is native from 62+. Here's the documentation for DevSettings module https://reactnative.dev/docs/devsettings#reload – Estevão Lucas May 13 '20 at 15:26
  • 1
    The documentation (https://reactnative.dev/docs/devsettings) states that it is 'for customizing settings for developers in development'. Do you use it in production? I tried it on iOS and the app closes (crashes?) instead of restart. – Yossi Aug 14 '20 at 20:03
  • 5
    `The DevSettings module exposes methods for customizing settings for developers in development.` – ggDeGreat Sep 22 '20 at 08:47
  • 10
    So this means that, this api does not work in production – olawalejuwonm Jan 19 '21 at 06:46
  • 2
    Please note that the docs mention `DevSettings` API only works in development. – heytulsiprasad Jun 21 '21 at 11:38
32

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.

Mike Martin
  • 3,850
  • 1
  • 15
  • 18
  • 2
    Restarting like this seems to leak memory though. Do a few restarts and the app will get killed quite fast. Still not sure why. – Cristiano Coelho Oct 01 '19 at 05:08
  • 2
    @CristianoCoelho That's correct. I've also noticed in my experience that it leaks memory. I'm not familiar with the technical details, but I believe react-native-restart likely also leaks memory because it's extracted from codepush. We see a higher incidence of OutOfMemoryErrors on Android after doing a restart via codepush – Mike Martin Oct 22 '19 at 23:24
14

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();
Estevão Lucas
  • 4,440
  • 34
  • 37
  • 10
    Although this works, it seems to only work on development builds (hence the DevSettings name, I guess). Building to a device through Xcode works, but uploading an archive to the app store / Test Flight crashes on this call. Any way around that (aside from using the react-native-restart package)? – mmccaff Oct 01 '19 at 13:27
  • 1
    It seems this works for Android as well, I don't know if it always did or an RN update did it, but I suggest you change your answer - I did not use it at first because I thought it's just for iOS. – MikeL Apr 02 '20 at 16:31
  • 6
    This does not work in release, but works in debug mode. Is there any suggestion for release mode? – Bumuthu Dilshan May 04 '20 at 06:15
  • 1
    For release mode you can use React Native Restart package (first answer) – Estevão Lucas May 18 '20 at 14:39
7

You can use ReactInstanceManager like this

final ReactInstanceManager instanceManager = getReactInstanceManager();
instanceManager.recreateReactContextInBackground();
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Mohit Goel
  • 722
  • 1
  • 8
  • 18
6

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();
sajad abbasi
  • 1,988
  • 2
  • 22
  • 43
  • I tried it with `expo` and I get an error `null is not an object (evaluating '_reactNativeRestart.default.Restart')`. There is an [issue](https://github.com/avishayil/react-native-restart/issues/100) with auto linking, when without expo. So I guess expo uses auto linking... – Fotios Tsakiris Jul 09 '21 at 16:10
  • 1
    @FotisTsakiris linking is not needed since react native v60 – sajad abbasi Jul 10 '21 at 12:29
5

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();
}
Jo E.
  • 7,822
  • 14
  • 58
  • 94
3

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

Estevão Lucas
  • 4,440
  • 34
  • 37