Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload page on a Cordova project?

I am building an app, using polymer starter kit & cordova to wrap the project. Now, since I use firebase as a database for storing data, I ended up using two native firebase javascript function to find user data:

getAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var authData = ref.getAuth();
if (authData) {
  console.log("Authenticated user with uid:", authData.uid);
}

onAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData) {
    console.log("Authenticated with uid:", authData.uid);
  } else {
    console.log("Client unauthenticated.")
  }
});

Those two functions require a reload to bring back data from firebase, but :

window.location.reload();

doesn't work

Alos looked for a Cordova plugin: webview-reloader, installed it but redirect and reload still not working.

When i use the reload() function the screen of my android phone is going white and the application stop working. Need to close the app and open it again.

enter image description here

like image 597
Dragod83 Avatar asked Oct 01 '15 10:10

Dragod83


People also ask

How do I run Cordova on my browser?

Working with the Browser Platform That means running cordova platform add browser . And if you want the latest, then you need to specify cordova platform add [email protected] . Soon that version will be the default, and if you're reading this in the future you should probably just take the default, it could 6.0. 0 by now.

What is Cordova InAppBrowser?

We can define the InAppBrowser as a native Cordova plugin that mainly used to add an in-app browser for any of the hybrid mobile applications. By using the InAppBrowser, we can open an external link from the Cordova application. It can easily load a webpage inside the Cordova app.


2 Answers

Since Cordova is a wrapper around a browser window:

window.location.reload(true);

It works for the browser windows as well as a Cordova app.

like image 169
Hgehlhausen Avatar answered Sep 27 '22 18:09

Hgehlhausen


This works for iOS and Android (at least with 2016 platform versions).

// 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;
}
like image 29
Sebastien Lorber Avatar answered Sep 27 '22 18:09

Sebastien Lorber