Phone BackKey Functionality in Android Phonegap - android

Please help me on this.....
I am using phonegap-1.1.0v . In my application i have multiple HTML file. Each file is a single page
Each page contains the home button on top of every HTML page.
when i moved from Homepage -- > screen B --> Again Homepage --> screen c
when i pressed Back key on Screen c it goes back to Homepage but when i press the Backkey again it goes to Screen B and the Homepage.
For MY solution i requires when i press the Back key from home screen it always need to Flash screen page (or) it should get exit from the application.
Thanks in Advance.............:)

Here is how you do
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('#homepage')){
e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);

//Deviceready function
document.addEventListener('deviceready', function() {
document.addEventListener("backbutton", go_back, false);
}, false);
function go_back(){
// Put your code when back key is press
// U can use window.location="home.html" to change page or something else
// If u want to exit then use "device.exitApp();"
}

For that you have to take a global Boolean variable and when you reach at home make it true and check this variable every time when you press back button like :
if (device.platform == 'android')
{
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e)
{
if(isHome)
{
navigator.app.exitApp();
}
else
{
navigator.app.backHistory();
//What you want when its not home page
}
}
}

Related

How to remove confirm navigation pop-up in mobile app

This happens when changes are made and redirecting to other pages especially during login or logout.
Code for ondevice ready is,
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
//alert("ready");
navigator.splashscreen.hide();
window.onbeforeunload = null;
}
Please suggest a way to hide this native pop up..
Add the following line in your code,
window.onbeforeunload = null;
This line need not be placed inside deviceready event listener. It can be placed on functions that it called during click of back button or during page reloads.

Back button handler in Cordova Android 4.0.0 App with crosswalk not preventing behavior

I'm using the following code to capture the back button press in a Cordova Android 4.0.0 App with Crosswalk:
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown(e) {
e.preventDefault();
alert("Back button pressed!");
}
The button handler is called but using e.preventDefault() is not working and the App is still being closed.
Does anyone know why or a workaround for this?

Sapui5 Back Button on Android Device

I have a sapui5 mobile application which runs perfectly. The android device has a back button. What I want to achieve is that when you press the button the app should navigate back.
In the sapui5 mobile api it is possible with this coding:
app.back();
The question now is how I can make this?
The activity of my android application has the following method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//Here you should navigate one view back
}
return super.onKeyDown(keyCode, event);
}
Is it possible to call from my activity the app variable in javascript and then tell my application to navigate back?
Greetings
Stef
I found a solution for solving the problem ... I wrote a new javascript file with the following code:
function onBackKeyDown(){
// Handle here the BackButton
}
function onDeviceReady(){
document.addEventListener("backbutton", onBackKeyDown, false);
}
function init(){
document.addEventListener("deviceready", onDeviceReady, false);
}
I took this out of the phonegap api and works perfectly!
Greetings
Stef

Handling Android back button in jQuery Mobile

I'm creating a jQuery Mobile app which is contained within a Titanium app's webview. When the Android back button is pressed, it exits the app rather than going to the previous page (not the best default behavior in my opinion.) I'm trying to get around this by executing the following code on app load:
document.addEventListener("deviceready", function () {
alert("adding back button event");
document.addEventListener("backbutton", function (e) {
alert("in back button event");
if ($.mobile.activePage.is('#homepage')) {
alert("exit app");
e.preventDefault();
navigator.app.exitApp();
}
else {
alert("go back");
navigator.app.backHistory()
}
}, false);
}, false);
Everywhere I see threads about this issue, they say to use the backbutton event, however it's not being fired at all. If I wrap it in a deviceready event, like above, then deviceready isn't called either. Is the webview somehow suppressing these events, or is there another way to do this with jQuery Mobile?
Your example will not work because you are trying to use Phonegap API call for backbutton handling.
You need to use something appropriate to Appcelerator. First you should check out the canGoBack, canGoForward, goBack and goForward functions for webViews.More info can be found here.
Basically you will need to create function that will handle a backbutton in your webview. Something like this:
detailContainerWindow.addEventListener('android:back', function(e){
if (detailView.canGoBack()) {
detailView.goBack();
} else {
detailContainerWindow.close();
}
});

how to handle device back button on sencha touch application

In Sencha touch if I use navigation view i can get back button. This is pretty fine.
But what if user hit device backbutton? it is direct exiting the applicaiton. In my requirement it should not exit the application it has to go back to previous screen.How can i do this?.
You can handle hardware back button like this:
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(eve) {
eve.preventDefault();
//do something
alert('back button pressed');
}
}
I didn't find the instructions on the history support page that useful when trying to do this; I couldn't see anyway to use routes when dealing with a navigation view which can have a large stack of views on it at anytime.
If you just want the back button to work though, you can use the popstate and pushstate functions (see https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history for a reference). The idea is that you push a state when adding a view and pop it off when removing one. The physical back button on an Android phone, or the back button on a desktop browser effectively calls history.back(); so all you need to do is ensure that pressing the back button on the titlebar does the same, and it is that which triggers the nav view to pop.
To make use it work in Sencha Touch, I add the following to the main controller:
In refs I have references to the main view (an instance of Ext.navigation.View) and to its titlebar, from which you can hook onto the event of the back button e.g.:
refs: {
main: 'mainview',
mainTitleBar: 'mainview titlebar',
}...
I attach the following functions via the control config object..
control: {
main: {
push: 'onMainPush'
},
mainTitleBar: {
back: 'onBack'
},
...
These are defined as:
onMainPush: function(view, item) {
//do what ever logic you need then..
history.pushState();
},
onBack: function() {
history.back(); //will cause the onpopstate event to fire on window..
//prevent back button popping main view directly..
return false;
},
I then attach a function to execute when the state is popped via the init function of..
init: function() {
/* pop a view when the back button is pressed
note: if it's already at the root it's a noop */
var that = this;
window.addEventListener('popstate', function() {
that.getMain().pop();
}, false);
},
Now, pressing back on the titlebar, executes history.back(), this in turn fires the popstate event which then causes the main view to pop.
If you want to see this working on a real application, there is a (v. basic!) property finder app using this technique on github here.

Categories

Resources