Phonegap : Back button not working - android

I have disabled the back button in menu.html page. Using this
i have successfully quit my app on pressing back button on phone.
But this causes back button not working on any other page.
function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
}
I want to navigate to Menu.html page when cliked on back button on phone. I have tried this but failed.
function onBackKeyDown(e) {
e.preventDefault();
navigator.notification.confirm("Are you sure you want to go to Menu page?", onConfirm, "Confirmation", "Yes,No");
// Prompt the user with the choice
}
function onConfirm(button) {
if(button==2){//If User selected No, then we just do nothing
return;
}else{
window.loacation="menu.html";// Otherwise navigate to Menu Page
}
}
how can i link to menu page when cliked on back button?

try to use window.location.replace("menu.html")

Related

Pressing back button on android breaks navigation logic when backstack is empty

I have had similar issues with back button before.
In my app I have a login page where the user submits their phone number and then they are taken to a page where they enter the verification code that hey have received. If the user pressed the back button on that page, the app gets minimized but when it's opened again the login page is shown instead of the page for submitting the verification code, even though I've specified clearhistory: true.
This is how I navigate:
this.$navigateTo(ConfirmSMS, {
transition: {name: 'slideLeft'},
clearHistory: true
});
You must use clearHistory only if you don't want use to go back to Login back upon pressing back button.
When you press back button and there are no Pages in back stack, application will terminate. It will still appear in recent application but unlike iOS tapping on the recent application will restart it unless it was paused but another activity / home button.
You may override back button to pause application instead of terminating it.
import { isAndroid } from "#nativescript/core/platform";
import * as application from "#nativescript/core/application";
import { Frame } from "#nativescript/core/ui/frame";
if (isAndroid) {
application.android.on(application.AndroidApplication.activityBackPressedEvent, function (args) {
const frame = Frame.topmost();
if (frame && !frame.canGoBack()) {
args.cancel = true;
var startMain = new android.content.Intent(
android.content.Intent.ACTION_MAIN
);
startMain.addCategory(android.content.Intent.CATEGORY_HOME);
startMain.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
application.android.foregroundActivity.startActivity(startMain);
}
});
}
Playground Sample

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).

How to go about login and logout in phonegap and JQM

Good day,
I am new to phonegap and JQM...
I have a problem with phonegap and JQM.i did a login and logout application but the backbutton on android being the secure page back after I logout, instead of exiting the application or do something else ... Secondly, how will I remove backbutton event in phonegap...thirdly, must I control the backbutton in my application?
In case you want backbutton to exit an app in one page and in other to act as a real back button you should use this:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
if($.mobile.activePage.is('#loginpage')){
navigator.app.exitApp(); // Exit app if current page is a login page
}
else {
navigator.app.backHistory(); // Go back in history in any other case
}
}
To remove/prevent a backbutton event use this:
document.addEventListener("backbutton", function(e){
e.preventDefault();
}, false);
And yes you must control your backbutton app event.

Back button event is not fired after soft keyboard hide event

I am developing an application, where there is a log-in page and I have included
document.addEventListener("backbutton", handleBackButton, false);
function handleBackButton() {
}
to handle back button event which works perfectly fine, but when I click on any input field softkeyboard is shown up and on pressing the back back button the key board gets down but when I click again on the back button the app crashes instead of calling the "handle backbutton" function
try this
#Override
public void onBackPressed() {
super.onBackPressed();
this.handleBackButton();
}

handling back button in android from jquery

We have developed an application in jQuery integrated with android (which has only one activity). From this activity we are loading an HTML file, on this file we are showing and hiding the div contents. On viewing the web pages provided here and pressing back button provided in android, the application exits. We want the application to go to the previous page on clicking the back button.
(On clicking the android back button we need to show the hidden div contents and on clicking the android back button from the div contents page the application should exit.)
Use this code in your HTML page to handle back button pressed
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
Try this:
function onDeviceReady() {
// Add this line to the on onDeviceReady() function
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle back button hardware
function onBackKeyDown(e) {
e.preventDefault(); // the default action of the event e should not be triggered.
history.back(1); // load the previous window.location
}

Categories

Resources