Back button handling behaving strangely...

932 views
Skip to first unread message

KenCorey

unread,
Jul 25, 2012, 9:50:42 AM7/25/12
to phon...@googlegroups.com
Hi All,

Having back button troubles, and I'm hoping someone can point out where I'm going wrong.

In my DeviceReady handler:

// handle the back button
document.addEventListener("backbutton", function(e){
console.log("Got a back button.");
if (backs.length>0) {
console.log("Got a back length.");
// see if there's something to cancel
var evt = backs.pop();
evt();
} else {
console.log("No back length.");
   if(!curscreen){
console.log("Home scren back button.");
       e.preventDefault();
       navigator.app.exitApp();
   } else {
console.log("Sub scren back button.");
    curscreen=0;
       drawtiles(curscreen);
   }
}
}, false);


Then my app shows the home screen (curscreen=0).  If the user presses the 'back' button now, my app exits.

If the user goes to another screen (curscreen>0), and then presses the back button, I trap the button event, and am sent back to curscreen 0.  So far, so good. This can be done as often as the user would like, and the behaviour is correct.

Now, I want to let the user type something.  I have a "dialog" defined like this, with a default style of display:none;:
<div id="finddialog">
<div class="dialogsection dlgtitle"><div id="dlgfindcancelbutton" class="button" onclick="cancelFindDialog();"><div id="dlgfindcancelbuttontxt">Cancel</div></div>Find</div>
<div id="dlgfindentry"><input id="dlgfindentrytext" type=text width=30 onsubmit="doFind();"></div><div id="dlgfindbutton" class="button" onclick="doFind();"><div id="dlgfindbuttontxt">Find!</div></div>
</div>

To show the dialog, my code sets the display to 'block'.  Great, it appears.  (It's not really a dialog box, it is just a div on the same page as before.)

The user taps on the input "dlgfindentrytext", the keyboard pops up and then the user can edit things, so far so good.

Oops, now they've changed their mind, and press the back key.  The keyboard disappears, but the entry has a flashing cursor still.  The user presses 'back' again.

No messages from the back button handler are printed in logcat.  I do have 'pause' and 'resume' handlers, and my app does a 'pause' (a no-op in my case...just writes a message to the log), and then my app just quits (logcat below).

In my manifest, I've got:
    <uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="16" />

In my Galaxy Tab 2 (4.0.4), the place where the 'back' button is turns into a down pointing arrow to dismiss the keyboard. Other than that, the behaviour is the same.

Very Interesting...my Nexus 7 (4.1.1) acts as I'd like it to act, where the 'backbutton' is captured correctly

Is there anything else I need to do to re-enable the backbutton just after the user has dismissed the keyboard?  If the user clicks my 'cancel' button to hide the dialog, and then goes to a subscreen, the back button is working again.

-Ken

W/SurfaceFlinger(   78): captureScreenImplLocked:H/W composition disabled
D/CordovaWebView(31102): >>> loadUrlNow()
D/SoftKeyboardDetect(31102): Ignore this event
D/CordovaLog(31102): pausing.
D/CordovaLog(31102): file:///android_asset/www/index.html: Line 169 : pausing.
I/Web Console(31102): pausing. at file:///android_asset/www/index.html:169
W/InputManagerService(  126): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@2d188ea8
(uid=10085 pid=31102)
D/dalvikvm(  293): GC_CONCURRENT freed 2380K, 29% free 12964K/18183K, paused 3ms+6ms
D/dalvikvm(  126): GC_CONCURRENT freed 1073K, 69% free 10968K/34567K, paused 3ms+30ms
W/InputManagerService(  126): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@2d188ea8
(uid=10085 pid=31102)
W/IInputConnectionWrapper(31102): showStatusIcon on inactive InputConnection
D/OpenGLRenderer(31102): Flushing caches (mode 0)
D/OpenGLRenderer(31102): Flushing caches (mode 1)
D/DroidGap(31102): onDestroy()
D/CordovaWebView(31102): >>> loadUrlNow()
D/CordovaWebView(31102): >>> loadUrlNow()
D/DroidGap(31102): onMessage(onPageStarted,about:blank)
D/Cordova (31102): onPageFinished(about:blank)
D/DroidGap(31102): onMessage(onPageFinished,about:blank)
D/DroidGap(31102): onMessage(exit,null)
D/dalvikvm(  191): GC_CONCURRENT freed 603K, 64% free 8688K/23943K, paused 1ms+4ms
W/ThrottleService(  126): unable to find stats for iface rmnet0
W/MobileManager--->MobileManagerDataStateTracker(  126): ignoring setPolicyDataEnable(true)






marques99

unread,
Jul 25, 2012, 3:26:29 PM7/25/12
to phon...@googlegroups.com
Here is a 'fixed' that I figured out.  In your java file add this override.

    @Override
    public void onBackPressed() {  //hack for Android 2.* Devices
        super.loadUrl("javascript:onBackKeyDown()");
    return;
    }

Put your backbutton code in the onBackKeyDown function.  Note, like you I do not see this issue with 4.* devices.

Marques

KenCorey

unread,
Jul 26, 2012, 12:36:10 PM7/26/12
to phon...@googlegroups.com
Ah Marques,  

You're a star!  If I could buy you a beer I would.

That really improves the feel of the app all around.  Well done!

-Ken 

Jamie Lemon

unread,
Dec 12, 2012, 6:50:28 AM12/12/12
to phon...@googlegroups.com
20 beers from me - this PhoneGap Android 2.0 problem was killing me .... :)

Sabariraj K

unread,
Jul 9, 2013, 5:02:32 AM7/9/13
to phon...@googlegroups.com
Thanks.

Reply all
Reply to author
Forward
0 new messages