Javascript 是否可以防止Cordova中的应用程序重新初始化?
我正在构建一个名为Webview的Cordova项目,并基本上使用它将我的网页显示为应用程序。我已经在Android上测试了我的代码,应用程序加载良好,页面加载良好。我可以浏览我的网站,并使用硬件“后退”按钮导航回初始页面。但是,当我进入初始页面时,如果我再次单击“后退”按钮,我会看到设备就绪屏幕,应用程序通常不会重新初始化。我已经在Android模拟器(Galaxy Nexus API 30)和我的个人Android设备(三星Galaxy S7)上进行了测试 我想知道的是,我是否可以阻止应用程序返回到设备就绪屏幕,或强制应用程序重新初始化,以便用户不会在设备就绪屏幕上“卡住”?我在Duck Duck Go、Google和Stack Overflow上进行了广泛的搜索,没有发现有人曾经解决过这个问题。请注意,我对Cordova和Javascript相当陌生,但我是HTML专家 以下是我的index.html文件中的应用程序代码:Javascript 是否可以防止Cordova中的应用程序重新初始化?,javascript,android,cordova,webview,back-button,Javascript,Android,Cordova,Webview,Back Button,我正在构建一个名为Webview的Cordova项目,并基本上使用它将我的网页显示为应用程序。我已经在Android上测试了我的代码,应用程序加载良好,页面加载良好。我可以浏览我的网站,并使用硬件“后退”按钮导航回初始页面。但是,当我进入初始页面时,如果我再次单击“后退”按钮,我会看到设备就绪屏幕,应用程序通常不会重新初始化。我已经在Android模拟器(Galaxy Nexus API 30)和我的个人Android设备(三星Galaxy S7)上进行了测试 我想知道的是,我是否可以阻止应用程
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Cordova App c15-iab</title>
</head>
<body>
<div class="app">
<h1>My Sample App</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
// Add event listeners.
document.addEventListener("offline", goOffline, false);
document.addEventListener("online", goOnline, false);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id),
listeningElement = parentElement.querySelector('.listening'),
receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
// BT next several lines of code. 20200907_1810
function goOffline() {
// Redirect to your local/offline page here
document.location = 'offline.html';
}
function goOnline() {
// Load the actual online content in InAppBrowser/WebView
//use some really slow page for testing. The http page has been whitelisted.
var path="http://www.someexamplesite.com";
// Using '_self' to force WebView open.
var ref = cordova.InAppBrowser.open(path, '_self', 'location=no');
//spinner html
var spinner ="<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width,height=device-height,initial-scale=1'><style>.loader {position: absolute; margin-left: -2em; left: 50%; top: 50%; margin-top: -2em; border: 5px solid #f3f3f3; border-radius: 50%; border-top: 5px solid #3498db; width: 50px; height: 50px; -webkit-animation: spin 1.5s linear infinite; animation: spin 1.5s linear infinite;}@-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); }}@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform:rotate(360deg); }}</style></head><body><div class='loader'></div></body></html>";
//intended webpage is loaded here
ref.executeScript({code: "(function() {document.write(\""+spinner+"\");window.location.href='"+path+"';})()"});
//loadstart event
ref.addEventListener('loadstart', function(event) {
//nothing specific needed for spinner
});
//loadstop event
ref.addEventListener('loadstop', function(event) {
//nothing specific needed for spinner
});
}
</script>
</body>
</html>
科尔多瓦应用程序c15 iab
我的示例应用程序
连接到设备
设备已准备就绪
变量应用={
//应用程序构造函数
初始化:函数(){
这是bindEvents();
},
//绑定事件侦听器
//
//绑定启动时所需的任何事件。常见事件包括:
//“加载”、“deviceready”、“脱机”和“联机”。
bindEvents:function(){
文件.addEventListener('devicerady',this.ondevicerady,false);
},
//deviceready事件处理程序
//
//“this”的作用域是事件。要调用“receivedEvent”
//函数,我们必须明确调用“app.receivedEvent(…);”
ondevicerady:function(){
app.receivedEvent(“DeviceRady”);
//添加事件侦听器。
文件。addEventListener(“脱机”,goOffline,false);
文件。附录列表(“在线”,在线,假);
},
//在接收到的事件上更新DOM
receivedEvent:函数(id){
var parentElement=document.getElementById(id),
listeningElement=parentElement.querySelector('.listening'),
receivedElement=parentElement.querySelector('.received');
setAttribute('style','display:none;');
setAttribute('style','display:block;');
console.log('接收到的事件:'+id);
}
};
app.initialize();
//BT下几行代码。20200907_1810
函数goOffline(){
//在此处重定向到本地/脱机页面
document.location='offline.html';
}
函数goOnline(){
//在AppBrowser/WebView中加载实际在线内容
//使用一些非常慢的页面进行测试。http页面已被列入白名单。
变量路径=”http://www.someexamplesite.com";
//使用“\u self”强制打开WebView。
var ref=cordova.InAppBrowser.open(路径“_self”,“位置=no”);
//微调器html
var spinner=“.loader{position:absolute;margin left:-2em;left:50%;top:50%;margin top:-2em;border:5px solid#f3f3;border radius:50%;border top:5px solid#3498db;width:50px;height:50px;-webkit动画:旋转1.5s线性无限;动画:旋转1.5s线性无限;}-webkit关键帧旋转{0%{-webkit变换:旋转(0度);}100%{-webkit变换:旋转(360度);}}@关键帧旋转{0%{变换:旋转(0度);}100%{变换:旋转(360度);}”;
//此处加载了预期的网页
ref.executeScript({code:”(function(){document.write(\“”+spinner+“\”);window.location.href='“+path+”;})()”);
//loadstart事件
参考addEventListener('loadstart',函数(事件){
//微调器不需要任何特定的设置
});
//加载停止事件
参考addEventListener('loadstop',函数(事件){
//微调器不需要任何特定的设置
});
}
有人能帮忙吗?我会尝试拦截backbutton事件,并在window.history.length降至1时取消它。只是不提供网页,而是将其设置为本地SPA(单页应用程序)…然后,设备的“后退”按钮显然将无法导航到任何地方,因为没有历史记录。也可以远程加载SPA,但唯一的问题是,这在脱机时不起作用。这就是为什么它应该打包而不是获取。亲爱的Martin,由于对javascript比较陌生,我不确定我是否有足够的能力构建SPA.Also、 我不确定这是否合适,因为我的目标是在我的InApp浏览器中提供远程网页,就像它是实际的应用程序一样。你还有其他想法吗?BrianDear weHe,我尝试在应用程序的onDeviceReady事件中添加“document.addEventListener”(“backbutton”,onBackKeyDown,false);”,然后添加了以下函数:[代码]函数onBackKeyDown(){alert('window.location.history='+window.location.history);if(window.loca