36

How can I programmatically restart my phonegap app? I exit the app easily through navigator.app.exitApp(), but I would like it to restart immediately.

The reason I need this, is I have to restart it after the user chooses his user language from a selectbox menu. The i18n immediately fires after the user updated his language choice, apart from 5 buttons on 1 page and a paragraph on another page. Those 5 items remain in the previous language (also the language select menu layout gets completely destroyed for some reason), but when I restart the application they're all in the correct language. There's either a very complicated problem with the i18n process, or I can backdoor solve it with the forced restart.

Ty, Nutty

NuttySquirrel
  • 413
  • 1
  • 4
  • 8

3 Answers3

49

I use the following:

location.reload(); 

Works a treat for me.

Paul

Paul Higgins
  • 1,126
  • 1
  • 14
  • 15
  • Oooo, I almost forgot about this question, since this functionality is removed in phase 1. But it does seem to work, so in a later phase it might be of great use. Thx – NuttySquirrel Apr 25 '13 at 09:34
  • 16
    This reloads the index page but it doesn't actually close and restart the app. Useful in it's own right. However, I am still wondering if a full restart is possible...? – Jason Oct 19 '13 at 01:35
10

Try this

navigator.app.loadUrl("file:///android_asset/www/index.html", {wait:2000, loadingDialog:"Wait,Loading App", loadUrlTimeoutValue: 60000});

Bakri

Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
user3210388
  • 127
  • 1
  • 2
10

This works for iOS and Android (at least with 2016 platform versions). It works also in SPA applications.

// keep startup url (in case your app is an SPA with html5 url routing)
var initialHref = window.location.href;

function restartApplication() {
  // Show splash screen (useful if your app takes time to load) 
  navigator.splashscreen.show();
  // Reload original app url (ie your index.html file)
  window.location = initialHref;
}
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • thanks for post just one line need to add after window.location = initialHref is that navigator.splashscreen.hide(); then it works perfect. – Pritish Feb 07 '17 at 06:29
  • 1
    @Pritish it would be better if the hide() was called directly when your index.html page reloads, otherwise the splashscreen would not be seen during the whole time the app is restarting. But if it works for you fine it depends on how you integrate splash screen into your own app – Sebastien Lorber Feb 07 '17 at 09:27
  • thanks for valuable reply I will make changes in code as per your suggestions. – Pritish Feb 08 '17 at 05:04