Search code examples
androidioscordovameteorcordova-plugins

Force full app restart in order to refresh packages in Cordova


I'm looking to restart the app completely from the app itself( really restart the app(full load) and not just rerender the index).

This needs to happen because some packages also need to re-initialize and this can only be done when fully restarting the app.

I've tried this package https://www.npmjs.com/package/cordova-plugin-exit which doesn't seem to work.

And navigator.app.exitapp() also isn't what i'm looking for or does this exactly do what I want it to do?

Is the best solution to create a cordova wrapper plugin which does this for android and IOS differently?


Solution

  • You can do this on Android with the restart() method of cordova-diagnostic-plugin:

    // Warm restart
    cordova.plugins.diagnostic.restart(null, false);
    
    // Cold restart
    cordova.plugins.diagnostic.restart(null, true);
    

    By default, a "warm" restart will be performed in which the main Cordova activity is immediately restarted, causing the Webview instance to be recreated.

    However, if the cold parameter is set to true, then the application will be "cold" restarted, meaning a system exit will be performed, causing the entire application to be restarted. This is useful if you want to fully reset the native application state but will cause the application to briefly disappear and re-appear.

    Note: There is no successCallback() since if the operation is successful, the application will restart immediately before any success callback can be applied.

    It is not possible to programmatically restart an app on iOS; at least doing so will likely get your app rejected from the App Store.