I am developing a sencha touch 2 application and I am packaging it using cordova for android. I want to handle the back button of android through cordova. I have written the below code in app.js file of my project, after adding all the views in the viewport.
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(e) {
e.preventDefault();
if (Ext.Viewport.getActiveItem().xtype == myHomeView.xtype ) {
navigator.app.exitApp();
} else {
Ext.Msg.alert("Back button is pressed");
}
}
}
Also, I built it using command line.
1. cmd --> cordova create app com.basicapp.app "App"
2. Deleted all the content inside "www" folder and replaced with my project's content.
3. cmd --> cordova platform add android
4. Installed device plugin from cordova documentation.
5. cmd --> cordova build
But I am unable to handle the backbutton in the device.It just exits the application in all the views. Is the device ready event not getting fired? Can anyone please explain me what am I doing wrong here.
I was able to get this working by doing the following:
Added this to my index.html
<script type="text/javascript">
document.addEventListener("backbutton", MyApp.app.nativeBack(), false);
</script>
Defined this function within my application
nativeBack: function() {
console.log("back button was pushed...");
// navigationView.pop(), etc...
}
I'm packaging using PG Build, but it should work the same.