如何在 Cordova 项目上重新加载页面?

How to reload page on a Cordova project?

我正在构建一个应用程序,使用 polymer starter kit & cordova to wrap the project. Now, since I use firebase 作为存储数据的数据库,我最终使用了两个原生 firebase javascript 函数来查找用户数据:

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.")
  }
});

这两个函数需要重新加载才能从 firebase 取回数据,但是:

window.location.reload();

不起作用

Alos 寻找 Cordova 插件:webview-reloader,安装了它,但重定向和重新加载仍然无法正常工作。

当我使用 reload() 函数时,我的 android phone 的屏幕变白并且应用程序停止工作。需要关闭应用再打开。

因为 Cordova 是浏览器的包装器 window:

window.location.reload(true);

它适用于浏览器 windows 以及 Cordova 应用程序。

这适用于 iOS 和 Android(至少适用于 2016 平台版本)。

// 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;
}