Android does not fire "deviceready" on a new page

773 views
Skip to first unread message

Jurgen B.

unread,
Nov 15, 2011, 6:18:20 AM11/15/11
to phonegap
Hi,

The problem is a follows (and is Android specific, it works properly
on iOS):
I have an app with multiple .html pages. The first page index.html is
executed as it should and at a certain moment I want to go to another
page named portalhome.html. I do this by executing the following
javascript:

database.insert(
'login',
["username", "password", "key", "portal"],
databaseLoginList,
function()
{
console.log('insert callback');
document.location = "portalhome.html"; //
window.location.href = "portalhome.html"; does the same
});
where database is a "class" I made which creates a query, starts a
transaction and executes the query. The function() { ... } is the
callback that is fired after the transaction is done. After this is
executed the app shows the HTML structure of the new page (i.e. the
hardcoded part) but the "deviceready" function is never called.

I've tried several things to locate the problem and it results in what
looks like phonegap.js not being executed properly on a consecutive
page.

For example:
function onBodyLoad()
{
if (typeof navigator.device == "undefined"){
console.log('navigator.device: ' +
navigator.device);
document.addEventListener("deviceready",
onDeviceReady, false);
} else {
console.log('navigator.device: ' +
navigator.device);
onDeviceReady();
}

}

shows "navigator.device: undefined". Then stops.

Another example:
function onBodyLoad()
{
console.log('onBodyLoad portalhome');
var intervalID = window.setInterval(function() {
if (PhoneGap.available) {
console.log('PhoneGap.available');
window.clearInterval(intervalID);
onDeviceReady();
} else {
console.log('NOT PhoneGap.available');
}
},
500
);
//document.addEventListener("deviceready",
onDeviceReady, false);
}
shows "onBodyLoad portalhome" and an endless list of "NOT
PhoneGap,available".

Both examples seem to point out that PhoneGap is either not started
properly and/or never fires its deviceready event.

There is an error shown when changing from index.html to
portalhome.html:

11-15 10:39:04.293: D/PhoneGapLog(279): file:///android_asset/www/js/phonegap-1.2.0.js:
Line 867 : JSCallback Error: Request failed.
11-15 10:39:04.293: I/Web Console(279): JSCallback Error: Request
failed. at file:///android_asset/www/js/phonegap-1.2.0.js:867

The javascript above for changing the page is in a callback after an
insert on the database, did I miss something essential there? Is there
some kind of function that needs to be executed before I do the
switch? Or is the callback function terminating the page too soon?

Note:
- using phonegap-1.2.0.js
- event IS fired on iOS (version 4.2 and 5.0)
- yes, I'm using the Android version of phonegap-1.2.0.js for the
Android app

Thank you for having a look at this.

Jurgen

Jurgen B.

unread,
Nov 16, 2011, 10:26:13 AM11/16/11
to phonegap
Well, I found the solution in another topic after digging through a
whole lot of "solutions" that didn't work for me.

It appears for Android the window.location.href = "something.html"
fails in several cases. Instead you need to use
navigator.app.loadUrl("file:///android_asset/www/something.html").
So in my case I made a function that checks whether we are running
Android or iOS and uses the loadUrl function for Android devices.

This also solved the JSCallback Error, however it does occur every now
and then (not crashing the app though).

Jurgen

Dewang Shah

unread,
Nov 23, 2011, 10:52:20 AM11/23/11
to phonegap
Nice one! Thanks, that just helped me out :-)
Reply all
Reply to author
Forward
0 new messages