Android Back Button causes crashing/closing - sometimes

1,901 views
Skip to first unread message

Henry

unread,
Aug 7, 2010, 7:16:18 PM8/7/10
to phonegap
Hello All,

I really hope someone here can help me with this, I'm getting closer
towards the end of the project and now I'm finding a very annoying
bug.

Pressing the Back key will close/crash my app about 50% of the time. I
am able to reproduce this issue using the example provided with
phonegap.

All I do is change:
<a href="#" class="btn large" onclick="watchAccel();">Watch
Accelerometer</a>
to:
<a href="wifisetup.html" class="btn large">Watch Accelerometer</a>

and run the emulator. I click on Watch Accelerometer which takes me
to wifisetup.html, I press the Back Key and it takes me to Index.html
as expected.
If I do this 10 times, 50% of the time I will have the app exit on me.

Has anyone ran into this issue? Am I doing something wrong? Is there a
work around?

Is there a way to disable the Back key? I would rather have no Back
key functionality then to launch an app with that issue. Of course I
would rather fix this bug some how.


Please help :)

-Henry
.

DevDanDen

unread,
Aug 9, 2010, 9:31:58 AM8/9/10
to phonegap
Have your tried using ADB to see what is happening when it closes?

I am getting a similar error/behavior - still trying to pin it down...
(I was able to reduce the problem by disabling animation - not a good
permanent fix though)

Henry

unread,
Aug 9, 2010, 10:11:15 AM8/9/10
to phonegap
Hey DDD,

I found that if you pull all instances of JavaScript out of your
index.html - the issue goes away.

rajesh

unread,
Aug 9, 2010, 6:05:13 AM8/9/10
to phonegap
For disabling back key, you can use device.overrideBackButton(). You
may also look at handling the back button events by registering a
listener for the "backKeyDown" event.

Luke

unread,
Aug 10, 2010, 11:59:17 AM8/10/10
to phonegap
If you are not talking about the phoens back key, go here for your
answer ... http://code.google.com/p/jqtouch/issues/detail?id=256

(assuming you are using jqtouch)

Henry

unread,
Aug 10, 2010, 11:53:02 PM8/10/10
to phonegap
Luke - Thanks but this isn't a jqtouch issue.


All - Rajesh was kind enough to drop me a line, he said:

-----------------------
In your deviceready / document ready function add the following
javascript code:

device.overrideBackButton();
document.addEventListener("backKeyDown", function(){
//do your back key specific handling
// - for example, navigate to a different page
}, false);

When you no longer want to override the back key, you can use the
following code:

device.resetBackButton();

--------------

Unfortunately, the code didn't work. Also, using just
device.overrideBackButton(); to override the backkey
didn't work aswell.

If anyone can get it to work please let me know.



On Aug 10, 8:59 am, Luke <oll...@gmail.com> wrote:
> If you are not talking about the phoens back key, go here for your
> answer ...http://code.google.com/p/jqtouch/issues/detail?id=256

Henry Levak

unread,
Aug 11, 2010, 12:20:05 AM8/11/10
to phonegap
debugger says the follwoing when trying to use the overrideBackButton function

08-10 21:18:42.885: DEBUG/PhoneGapLog(2980): file:///android_asset/www/index3.html: Line 21 : TypeError: Result of expression 'device.overrideBackButton' [undefined] is not a function.


--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

Henry

unread,
Aug 11, 2010, 2:59:44 AM8/11/10
to phonegap
Ok I have the solution. First - BIG THANK YOU to Rajesh who helped me
figure out what was going on.


None of Rajesh's suggestions were working because my phonegap.js was
missing backkey support.
If you have this same issue, look inside your phonegap.js and you
should see:

Device.prototype.overrideBackButton = function()

and other BackButton support functions.

If your phonegap.js doesn't, goto phonegap.com and download the latest
full build form github,
I used the 0.9.1 download because edge was/is messed up. Go to the
github link instead and
click on "Download Source"

Look inside the new phonegap.js and make sure the BackButton stuff is
in there.
Recompile your project with the new phonegap files.

Now here is the code I'm using to manually set the Back Button to go
back:


document.addEventListener("deviceready", function(){
device.overrideBackButton();
document.addEventListener("backKeyDown", function(){
history.go(-1);
}, false);
}, false);


I have no crashes. Works perfectly. Let me know if anyone has any
questions on this.

Thanks Rajesh!!






On Aug 10, 9:20 pm, Henry Levak <henryle...@gmail.com> wrote:
> debugger says the follwoing when trying to use the overrideBackButton
> function
>
> 08-10 21:18:42.885: DEBUG/PhoneGapLog(2980):
> file:///android_asset/www/index3.html: Line 21 : TypeError: Result of
> expression 'device.overrideBackButton' [undefined] is not a function.
>
> > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>

Henry Levak

unread,
Aug 11, 2010, 3:04:46 AM8/11/10
to phonegap
sorry for spam, one more thing. current phonegap.js lacks Menu button support,

you need to paste:

KeyEvent.prototype.menuTrigger = function()
{
  var e = document.createEvent('Events');
  e.initEvent('menuKeyDown');
  document.dispatchEvent(e);
}

into phonegap.js

otherwise no menu :(

good night all.

Komal

unread,
Sep 21, 2010, 8:18:38 AM9/21/10
to phonegap
Hello Henry,

I am trying also trying to encounter the device back in my app and
prevent the close of the app.

but when I add this script tag <script type="text/javascript"
charset="utf-8" src="phonegap.js"></script> in my index.html page
I get this error Failed to run constructor:result of expression
'origObj' [undefined] not an object.

If I remove the phone gap script tag the app runs flawless in android
avd. :(

and I am writing this event listener in a method that is invoked on
load of the script in the bottom of the body section in index.html
page

I am not clear as to the definition of the device variable..

document.addEventListener("deviceready", function(){
device.overrideBackButton();
document.addEventListener("backKeyDown",
function(){

de.hotornot.jil.Main.getInstance().back();
}, false);
}, false);

please let me know what I should do to get the backkey down working..

thanks,
Komal

Pablo

unread,
Sep 27, 2010, 5:10:19 PM9/27/10
to phonegap
Henry,
I don't know if you are still checking this thread, but I am
encountering the same issue as you did. The BackButton does not work
for me. I saw your post were you downloaded 0.9.1 from gitHub and
found Device.prototype.overrideBackButton = function().

I have 0.9.1 and I can not find this line. I have tried to download it
from gitHub and the most recent there is 0.9.0. Can you post the
phonegap.js file here so I can get the code?

Or can someone tell me where I can get a version of phonegap.js that
has the overrideBackButton.


Thanks,
-toe

Henry Levak

unread,
Sep 28, 2010, 1:36:30 AM9/28/10
to Pablo, phonegap
Hey Pablo, yeah I can't even download Edge from their repo.. I'm attaching my phonegap.js

phonegap.js

Henry Levak

unread,
Sep 28, 2010, 1:37:21 AM9/28/10
to Pablo, phonegap
@Komal - try using my phonegap.js file - maybe thats the issue?

Komal

unread,
Sep 28, 2010, 11:33:44 AM9/28/10
to phonegap


Thanks Henry.. Got the issue with importing the phonegap.js working..

and the back key also works now.. :)

regards,
Komal


On Sep 28, 10:37 am, Henry Levak <henryle...@gmail.com> wrote:
> @Komal - try using my phonegap.js file - maybe thats the issue?
>
>
>
> On Mon, Sep 27, 2010 at 10:36 PM, Henry Levak <henryle...@gmail.com> wrote:
> > Hey Pablo, yeah I can't even download Edge from their repo.. I'm attaching
> > my phonegap.js
>
> > On Mon, Sep 27, 2010 at 2:10 PM, Pablo <pablitoal...@gmail.com> wrote:
>
> >> Henry,
> >> I don't know if you are still checking this thread, but I am
> >> encountering the same issue as you did. The BackButton does not work
> >> for me. I saw your post were you downloaded 0.9.1 from gitHub and
> >> found Device.prototype.overrideBackButton = function().
>
> >> I have 0.9.1 and I can not find this line. I have tried to download it
> >> from gitHub and the most recent there is 0.9.0. Can you post the
> >> phonegap.js file here so I can get the code?
>
> >> Or can someone tell me where I can get a version of phonegap.js that
> >> has the overrideBackButton.
>
> >> Thanks,
> >> -toe
>
> >> --
> >> You received this message because you are subscribed to the Google
> >> Groups "phonegap" group.
> >> To post to this group, send email to phon...@googlegroups.com
> >> To unsubscribe from this group, send email to
> >> phonegap+u...@googlegroups.com<phonegap%2Bunsubscribe@googlegroups.c om>

Pablo

unread,
Sep 28, 2010, 1:16:37 PM9/28/10
to phonegap
Henry,
Thanks for the js file. I am still not getting it to work. I am using
the following code which an adaptation of what you posted earlier.

document.addEventListener("deviceready", function(){

device.overrideBackButton();
document.addEventListener("backKeyDown", function(){
//history.go(-1);
alert("back button");
}, false);
}, false);

The "deviceready" is being called but once the
device.overrideBackButton() is called I get an error. I don't know how
to debug using my device so I am not sure what the error is. Would it
be possible for you to post a copy of your phonegap.0.9.1? I figure
that since the version I have is not the correct one maybe yours would
be. I know this should be a very simple thing to do but I am at a loss
as to why I can not get it to work.

Any help would be great.

Thanks,
-toe

andrew

unread,
Sep 30, 2010, 10:43:30 AM9/30/10
to phonegap
Hi Everybody,

My problem is the app seems to crash on backbutton close (at least it
won't access the sqlite database, but displays correctly).

I've downloaded phonegap0.9.1 which didn't work. Used the phonegap.js
from above with example code which didn't work either.

Also used the cloned git version which doesn't seem to have the
BackButton stuff in phonegap.js.

I'm not exactly sure what's going on because I can't see 0.9.1 in git
(in the tags at least).

Hopefully somebody can clear this up or point me in the right
direction.
Reply all
Reply to author
Forward
0 new messages