首页 文章

当我的cordova / phonegap应用程序从插件活动中恢复时,为什么onready事件会被触发?

提问于
浏览
1

使用cordova-plugin-camera的简单相机应用程序,但使用cordova-plugin-barcodescanner时以及直接使用phonegap而不是cordova时会发生完全相同的行为:

JS / index.js

var app = {
    initialize: function() {
        alert('init');
        document.addEventListener('deviceready', app.onDeviceReady, false);
    },
    onDeviceReady: function() {
        alert('deviceready');
    }
};
app.initialize();

function capturePhoto(){
    navigator.camera.getPicture(
      function(data) { alert('success'); }, function(error) { alert(error); },
      {sourceType:1, destinationType: Camera.DestinationType.FILE_URI}
    );
}

的index.html

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *">
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
</head>

<body>
<form>
    <button id="shoot" onclick="capturePhoto();">Take photo</button>
</form>
</body>
</html>

现在我的基本问题是,虽然相机活动完全正常,但我从插件中保存的照片不会返回到应用程序 - 事实上,既没有调用navigator.camera.getPicture的错误也没有调用成功回调 . 相反,我的应用程序被重新加载(虽然活动没有被销毁),因此重新加载html页面,我的app.initialize()函数被调用,事件被反弹 . 这意味着我无法访问我使用的插件的结果 . 此外,因为主要活动实际上没有被破坏,但仍然在后台活动,这里描述的技术不起作用:https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html#lifecycle-guide

为什么从android插件恢复时会调用 deviceready 事件?

1 回答

  • 1

    想出了什么's happening. Because I put the button inside a form, I guess even though it'不是它提交表单的提交按钮,它正在重新加载页面 . 解决方案是删除 <form> 标签 .

相关问题