Stop the back button from closing the Cordova application

I've encountered a persistent issue in my cordova application where the back button consistently exits the app, regardless of any attempted solutions. I have diligently searched for answers online and tested various strategies, but none have proven successful.

Every solution I've implemented (as shown below) yields the same outcome.

document.addEventListener("backbutton", onBackKeyDown, false);

function onBackKeyDown() {
    // Handle the back button
}

The code within my callback function executes smoothly, but triggers an app exit afterward. While introducing a ReferenceError in my function can prevent the exit, it doesn't align with best practices.

console.log(undefinedVar);

Additional attempts to remedy the issue have involved utilizing event.preventDefault() within the callback and implementing ionic's registerBackButtonAction function.

If anyone has any recommendations or insights, they would be greatly appreciated.

Answer №1

insert code here 

<!DOCTYPE html>
<html>
  <head>
    <title>Stop Back Button</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script>
        function getTitle() {
            document.getElementById("ct").innerHTML = "DEMO: " + document.title;
        }
    </script>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">
    // Wait for device API libraries to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
        getTitle();
    }
    // device APIs are available
    //
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
    // Handle the back button
    //
    function onBackKeyDown() {
        alert('Back Button Disabled');
        console.log('Back Button Disabled');
    }
    </script>
  </head>
  <body onload="onLoad()">
  <ul id="nav">
        <li><a href="index.html">&larr; Back</a></li>
        <li><a href="#" id="ct" onclick="location.reload();"></a></li>
    </ul>
  </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most effective approach for managing nested callbacks in Node.js/Expressjs?

My server side coding is done using Node.js and Expressjs, with MongoDB as the backend. Although I am new to these technologies, I need to perform a list of actions based on requests. For example, in user management: Check if the user is already registe ...

Using Android to access RESTful APIs

After successfully creating a rest API using Spring Boot which can add and retrieve data in JSON format, I am now facing an issue while trying to consume it with an Android client. Although the execution runs smoothly without any exceptions, nothing is bei ...

How can I use query to swap out elements within a div when clicked?

I have a project with two separate div classes named demo-heart and demo-coal. The goal is to implement functionality that allows the user to click on the fa-heart icon and have it switch to fa-coal, and vice versa when clicking on fa-coal. <div class ...

Declare the variable as a number, yet unexpectedly receive a NaN in the output

I'm facing an issue with a NaN error in my TypeScript code. I've defined a variable type as number and loop through an element to retrieve various balance amounts. These values are in the form of "$..." such as $10.00 and $20.00, so I use a repla ...

The behavior of TypeScript class inheritance differs from that of its ES6 equivalent

I'm currently developing a custom error handling middleware for my node.js project using express and TypeScript. One key component of this middleware is the AppError class, which extends the built-in Error class. The code for this class is as follows: ...

Error: Attempting to access a property of an undefined object using method chaining

I encountered an error of property undefined with the code below. I'm not sure what's causing it. I checked by doing a console.log(navList) in the render and it does have a value. Even after adding if(!navList) return null, I still get the same e ...

Obtain a Compilation of Video Sources in an HTML5 Format

Hey there, I am using an HTML5 video. This is just an example <video width="320" id="video" height="240" controls> <source src="video.mp4" type="video/mp4"> <source src="video1.mp4" type="video/mp4"> <source src="movie.ogg" typ ...

Repeated firing of jQuery's Ajaxstop within a click event

When using the quantity plus button on the woocommerce cart page and reaching maximum stock, I want to display a notice. However, due to an auto update on the cart, I have to wait for the ajax load to complete before showing the notice. My script revealed ...

jquery and radio button technology

I am attempting to create a basic function using jquery, but it is not functioning properly. Here is the radio button in question: <input type="radio" id="price" name="shipping" class="styled" /> In the head section of my HTML, I have included the ...

Dealing with the element not present error in Protractor can be managed by using various

Is there a way to achieve similar Exception handling in Protractor as we can with Selenium webdriver in Java? When dealing with element not found exceptions, what is the most effective approach to handle them using Protractor? ...

Utilizing JQuery Template to Invoke JavaScript Functions

If I have the following JSON object structure: ITEMS is an array with one element, and FILTER is another array with 3 items in it. Description: "churches with some restrictions" ITEMS: {...} [0]: {...} FILTER: {...} ...

Deliver integers using Express.js

When I try to send a response with Express using the code below, it doesn't work: res.send(13245) An error message is displayed: express deprecated res.send(status): Use res.sendStatus(status) instead src/x.js:38:9 (node:25549) UnhandledPromise ...

NodeJS domain error handling failing to catch ReferenceError exceptions

I'm currently facing some challenges with domains in NodeJS. I've set up middleware functions to wrap every call in my web application within a domain in order to capture errors, process them, and return informative error messages. While this se ...

Hyphens make Tablesorter sort numeric fields uniquely

I have a table where data is displayed in the format of YYYY-####. Sometimes, there are values like 2012-456 and 2012-1234. By default, the sorting places 2012-1234 before 2012-456. If I change the sort to 'numeric', it disrupts the order of othe ...

Identify whether the final digit falls within the range of 1-4 or 5-9, and then apply styling to the corresponding

First, this is the structure of my table: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <table class="table group-9569" width="100%"> <tbody> <tr> <td class=" ...

Implementing CSS styles according to user preferences. Switching between dark mode and light mode based on subscription

Is there a way to dynamically change CSS property values based on user interaction, such as toggling between dark mode and light mode? I am currently exploring the option of setting up a subscription to track these changes, but I want to know how I can act ...

What is the best way to merge two tables together using the server-side JQuery Datatable plugin?

I recently came across an amazing example of a server-side ColdFusion jQuery datatable on this website: Check it out here However, I am facing an issue with adding a second table in the lookup. Specifically, while the primary table lists location_id, I al ...

Traverse through the JSON data until a certain condition is satisfied, then proceed to tally the number

Looking to parse a json file containing user data and points. The goal is to loop through the data until the _id matches the userId, then determine the position of that user in the list based on the number of objects. The json file provided below already ...

Traverse the array of nested JSON objects in an iterative manner

I need to iterate through a JSON array object with the following structure: var myJSONObject = { "abc": { "prod_1": [ {"prod_ver" : "prod 1 ver 1"}, {"prod_ver" : "prod 1 ver 2"}, ], "prod_2": [ ...

What could be causing TypeORM skip() and take() function to not function properly?

I have encountered a database issue with the Delivery table, which contains more than 4 million rows in MySQL. The following function, adminDeliveriesViewCount, is intended to count the total number of deliveries, but it's not functioning as expect ...