Back button with Cordova/phonegap - android

I am creating an application using the framework cordova. I need to intercept the "Back" button of the phone itself in order to perform the various aoperazioni, for example closing a modal dialog. The problem is that now, once you open the modal, I can not close it by pressing the back button. What happens is that you close any application. How could I do to intercept this event which is not part of the app but the phone? I refer to the actual button of a smartphone. Thanks for your help.

You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", function (e) {
// your code
} , false);

Have you read the docs?
Here's what you need: http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#backbutton

Related

Override done button functionality inappbrowser

Is it possible to override done button functionality? i need to just hide inappbrowser on done button as when show same the last visit url next time.
First, you can hide the Done button in Android (setting option Footer = false) but not in iOS. This makes sense because in Android you have the Back button to close the browser and come back to the app. But iOS does not have back button, so the user needs a graphical button for it.
Second, you can't override the button functionality, but you can add events to the browser, to run your code when something happens, for example when the browser is closed, all of this is explained in the Github page:
https://github.com/apache/cordova-plugin-inappbrowser
browser. addEventListener( "exit", () => {
// Here do something
}) ;
Add closebuttoncaption=Done
Example:
cordova.InAppBrowser.open(url, '_blank', 'location=no,toolbar=yes,toolbarposition=top,closebuttoncaption=Done);

PhoneGap on click backbutton simulate click in home button

I'm looking for change action for backbutton, actually I'm using events to catch backbutton calls and using navigator.app.exitApp(); to close app (in really pause app), but I need backbutton call action like home button.
Why? Well, I using cordova-plugin-background-mode, and this works only when app send to background (if using navigator.app.appExit() app is paused), so I need leave app every in background.
Is possible change action of backbutton to works like home?
I want use in iPhone and Android systems
you can use the plugin
https://github.com/amitsinha559/cordova-plugin-back-as-home
and
https://github.com/tomloprod/cordova-plugin-appminimize
after adding the plugin you can minimize the app when ever you want
like on back button click
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
window.plugins.appMinimize.minimize();
}
Whichever suits you. Unfortunately they both are for android. you need to create plugin manually for iPhone.

How can i detect home pressed event in phonegap

I would like to close the app when the physical home button is pressed - does anyone know how to detect this?
.I try onPause event, but i have Social Shearing option in my application. After clicking dedicated button for shearing, Social Activity starts and my application going to close
After lot of searches, I did't find answer. There is no any event in phonegap for Home button press.
Home button work as a resume in android, you can add listener and implement it:
document.addEventListener("resume", function(){
//handle event here
}, true);

Close the application on back button of android phones

I'm developing a app using angularjs and ionic framework. It has a logout option also. When i logout from my app it goes straight to the home page.But if i press the back button it'll go to page that can be accessed only if you are logged in. How can i create a option to close the app if you press the back button from the home page? I use angularjs.
I used this way but did not work
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('/templates/playlists')){
/*
Event preventDefault/stopPropagation not required as adding backbutton
listener itself override the default behaviour. Refer below PhoneGap link.
*/
//e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
Your code is good.
But, I'm not sure about the second line : are you sure that the active page id is /templates/playlists ?
Normally, the value you should pass to the $.mobile.activePage.is function should contain a page id (which could be retrieved like this : $.mobile.activePage.attr('id')).
(by the way, I don't know which of jQuery Mobile you are using, but the $.mobile.activePage.is was deprecated).

Disable device back button click in OpenUI5?

How can i disable android device back button click in Phonegap OpenUI5 application? Tried this but didn't work:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
document.addEventListener("backbutton", function (e) {
e.preventDefault();
}, false );
}
Generally speaking, browsers don't allow you to disable the back button. This is for security/usability reasons so that you can't trap the user on your webpage. Hence, you need to think twice before doing it to make sure that you're actually trying to help your user.
That having been said, there are techniques described here that might work for you.
I would personally suggest that you do the right thing and rather than disable the back button you simply handle the event and do the appropriate clean up in your app.
In SAPUI5 you can use jQuery to listen to navigation events:
// event fired when the user changes the URL, e.g. forward or back navigation
// you need to implement the handleNavigate handler
$(window).on("navigate", handleNavigate)
// event fired when the user tries to close the browser or reload the page
// you need to implement the handleBeforeUnload handler
$(window).on('beforeunload', handleBeforeUnload);

Categories

Resources