6

I am writing an app with phonegap and jquery mobile.

For handling unknown errors I need to completely restart my app and reinitialise all variables and dynamic content back to being the equivalent of the first run.

Originally I was just using $.mobile.changePage($'#home') which obviously doesn't work because it doesn't reinitialise any of the dynamic content or variables so I end up with double ups.

Is there a method I can call in jquery mobile to completely restart the app and set everything back to the initial settings?

4 Answers 4

8
+50

For a full solution, I think you are out of luck.

iOS definitely will *NOT allow the programatic killing of an app, let alone the programatic starting one.

Android does allow the killing of an app. Though undocumented (that I could find) you can use

navigator.app.exitApp();

to kill the current app.

However, you still cant re-start it programaticly.

I think your best bet will be to write the code to reset the variables and local storage/DB (if used). Then reload the index page via document.location.href = 'index.html?var=xxx'; where xxx = the current timestamp. Placing the timestamp at the end will ensure that it pulls a non-cached version of the page.

*Edit: Added not to my statement about Apple allowing app killing as it should have been.

2
  • nice tips both with navigator.app.exitAPp() and timestamping href.
    – frequent
    Sep 14, 2013 at 13:41
  • I just got notified that a lot of people are reading this thread so I thought I would update a little bit. Killing the app caused problems with IOS and also causes problems if you are using cordova plugins that are active in the background so I actually ended up writing a manual reset function that goes through and sets all variables back to default. Not ideal or pretty but works well functionally.
    – Jamesla
    Jan 28, 2015 at 20:16
4

Try, document.location.href

document.location.href = 'index.html';
1
  • document.location.href = 'index.html'; not working in ios
    – Pritish
    Feb 6, 2017 at 11:30
2

Have you tried to reload app?

The reload() method is used to reload the current document.
The reload() method does the same as the reload button in your browser.
By default, the reload() method reloads from the cache, but by you can force the the reload to get the page from server by setting the forceGet parameter to true.

location.reload();
0

Instead of restarting the app, you can check for the pagebeforeshow of a particular page and intialize to what ever values you want. so that each time you visit that page the values will be intialized.

$.mobile.changePage( "#PageID", {
     reverse: false,
    changeHash: false

});


$(document).on('pagebeforeshow','#PageID',function(){

/* Intialise all your variables here*/


});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.