von c.kuetbach am September 5, 2012

App Closes if Bask-Button is pressed while focussing input-element

I just encountered a interesting Bug within a Cordova/Phonegap App:

Everytime a user enteres something into a input-field, the softkeyboard will show up. But if the user hides the keyboard and than hit the Android-Back-Button the application closed.

No errormessage on screen, nothing strange in the log. Just a clear shutdown of the App.

If the user hides the keyboard, unfocus the input-field and hits back, the app worked as designed.

After some time I found the problem: The Bback-Button will end the DroidGap Activity, that closes the app.

This ISSUE at the Codova Issue-Tracker described a similar problem: Cordova Issue Tracker

I found a simple solution there. Just override some native Key-Events in you DroidGap-Activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    LOG.d(TAG, "KeyDown has been triggered on the view"+keyCode);
    //If volumedown key
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { 
        // only override default behaviour is event bound 
        LOG.d(TAG, "Down Key >Hit"); 
        this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');"); 
        return true; 
    }
    // If volumeup key
    else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { 
        LOG.d(TAG, "Up Key Hit"); 
        this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');"); 
        return true; 
    } else { 
        //return super.onKeyDown(keyCode, event); 
    }
    //return super.onKeyDown(keyCode, event);
    return true;
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event){
    LOG.d(TAG, "KeyUp has been triggered on the view"+keyCode);
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
        this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');"); 
        return true; 
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) { 
        this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');"); 
        return true;     
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) { 
        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');"); 
        return true; 
    }
    return false;
}

Ein Kommentar zu "The Android-Back-Button stops a Cordova App"

#13
Hafid sagte:
December 29, 2012 at 01:26 am

Hello,I was google-ing for How to prevent auto closing of an app (written in AIR) and by chance come here. Your idea was the thing which I was using but with no success. Finally I manage to find the problem and the correct solution. Here it is:

systemManager.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onBackButtonPressed);
protected function onBackButtonPressed(event:KeyboardEvent):void
{
    if(event.keyCode == Keyboard.BACK){event.preventDefault();
        //prevents auto closing of your application if you are writing Flex based app
    }
}

I hope that it will help anyone.

Konnentieren

Ihre E-Mailadresse wird nicht publiziert.

Sie können die Markdown-Syntax nutzen.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.