Stopping of Cordova Android Application Event

In the process of developing a Cordova application through Visual Studio, I have encountered an issue on Android. Specifically, I am trying to implement a detection mechanism for when the user closes the app by performing a long press on the device's menu button followed by a swipe gesture. Unfortunately, the standard events provided by Cordova (onPause/onResume) do not seem to work for this purpose.

Although the onPause event triggers when I tap the device's home button, I have attempted utilizing both document.body.unload and window.unload events without success on Android. Surprisingly, these events function properly on Windows Phone but fail to work on Android.

I also explored using a Cordova plugin from https://github.com/agamemnus/cordova-plugin-ondestroy to capture the desired event, but it appears that the plugin is outdated as indicated by the "class not found" error in the JavaScript console.

If anyone can provide assistance or guidance on how to achieve the intended functionality, it would be greatly appreciated. Thank you.

Answer №1

Although this question may be considered old, the underlying issue remains relevant to this day. In response, I have developed a plugin that addresses this problem effectively. This plugin is designed to listen for the onWindowFocusChanged native event on Android, which triggers when the activity loses or gains focus. It ensures that the event is fired even when the menu button is pressed. Additionally, it includes functionality for handling focus loss/gain events on iOS, catering to scenarios such as opening the iOS app switcher.

If you're interested, you can find the plugin here: https://github.com/distinctdan/cordova-plugin-extra-events

However, it's worth noting that on Android, there is still a possibility for users to swiftly open the app switcher and terminate the application before the event can fully execute.

Answer №2

When it comes to addressing a specific aspect of your inquiry, I can certainly relate as I encountered a similar issue during the development of my hybrid mobile application involving unload events. Here is how I successfully implemented unload events in both iOS and Android platforms:

iOS:

$(window).unload(function() { ... }

Android:

$(window).bind('beforeunload', function() { ... }

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

The autocomplete feature in the remember me function is failing to work properly with jQuery

$(function() { if (localStorage.chkbx && localStorage.chkbx !== '') { $('#modal_login_remember').attr('checked', 'checked'); $('#modal_login_email').val(localStorage.usrname); $('#modal_ ...

Ways to select specific columns instead of all columns in a dataset (Extended Inquiry)

This question is a continuation of the previous inquiry. View Previous Question My objective now is to tweak the answer provided earlier. Specifically, I am looking to extract only certain columns instead of all. How should I go about making this adjustm ...

Adjusting the visibility of a div as you scroll

I'm trying to achieve a fade-in and fade-out effect on div elements when scrolling over them by adjusting their opacity. However, I'm facing difficulties in getting it to work properly. The issue lies in the fact that my div elements are positio ...

What is the best way to calculate the total of a field with matching Project and Employee names?

My task involves grouping data from an Array of objects by Project Name and Employee Name, whereby existing projects and employees have their hours added together. projects = [ { "project": { "id": 1, "name": "M ...

Transforming a d3 line chart into a bar chart

Beginner in D3 seeking assistance. I have successfully created a line chart that meets my requirements. Below is the code I used, with some modifications, mostly inspired by this source: <!DOCTYPE html> <meta charset="utf-8"> <sty ...

Tips for creating a dashed border line that animates in a clockwise direction when hovering

I currently have a dashed border around my div element: .dash_border{ border: dashed; stroke: 2px; margin:20px; } My goal is to make the dashed lines move clockwise when my pointer hovers over the div element and stop moving ...

Do not need to refresh the form

I developed a PHP-based Feedback form that includes a Popup from Foundation 5. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style type="text/css"> .auto-style1 { margin-left: 1px; ...

Tips for sending data scraped from a website using Express

I have created a web crawler using Axios and am attempting to upload a file through Express. I currently have 10 different crawlers running with corresponding HTML form methods in Express. However, when I click the button, it downloads a blank file first ...

Setting up Authorization Components in Redux and React

I've been encountering a major issue with my React/Redux authorization configuration and I'm completely stuck. The current error message is: HomeHeader.js?8595:26 Uncaught ReferenceError: dispatch is not defined There's a lot that's n ...

Troubleshooting issue with jquery.i18n.properties functionality

I am encountering an issue with implementing jQuery internationalization. I have included the files jquery.i18n.properties.js, jquery.i18n.properties-min.js, and jquery-min.js in my resources folder. In my jsp, I have added the following code: <script ...

Issue encountered while configuring 'innerHTML' in xmlHttp.onreadystatechange function

Trying to create a JavaScript function that changes the innerHTML of a paragraph within an xmlHttp.onreadystatechange function, I encountered an error in the Chrome Console: Uncaught TypeError: Cannot set property 'innerHTML' of null at XMLH ...

Discovering the right row to input the data and successfully integrating it: What is the best

Below is the code I am using to add a new task: <fieldset> <legend>Create New Task</legend> <input type="text" ng-model="subtaskname" placeholder="Subtask Name" ><br /> <select id="s1" ng-model="selectedItem" ng-options=" ...

What is the best way to determine the accurate size of a div's content?

There are 2 blocks, and the first one has a click handler that assigns the block's scrollWidth to its width property. There is no padding or borders, but when the property is assigned, one word wraps to the next line. The issue seems to be that scrol ...

Navigate to the top of the page using Vue Router when moving to a new

Currently, in my Vue application, whenever I click on a <router-link>, it directs me to the new page but at the same scroll position as the previous one. This happens because the link only replaces the component. I am curious to know if there is a s ...

Exploring Angular2: Implementing oauth2 with token headers

As someone who is new to Angular 2, I found that things were much simpler with interceptors in version 1.*. All you had to do was add them and suddenly your headers were available everywhere, making it easy to handle requests especially when dealing with i ...

Is it possible to have separate click functions for the onclick attribute and jQuery click handler on an anchor tag, instead of just calling one function through onclick?

Attempting to implement multiple click handlers for an anchor tag: one using the "Onclick" attribute handler and the other using a jQuery click handler. This excerpt is from my HTML file. <html> <head> <script src="http://code.jquery.com ...

Exploring XML with Jquery

Looking for assistance with parsing my XML Feed: Struggling to utilize JQuery to extract and display the data in HTML. Despite searching on Google, I haven't found a satisfactory solution yet. With numerous elements in my XML, it's proving to b ...

When an array is added to a Json Object in Jquery, the result is an array of arrays instead of a single array of values

Struggling with adding an array to a JSON object from two separate select lists, resulting in nested arrays instead of one unified array. Let me elaborate: The desired output is achieved when two items are selected from a single select list. In this scena ...

The scope of this variable in Node.js results in an undefined response

Have you ever noticed the difference in behavior when executing JavaScript code in Google Chrome's console compared to running the same logic in a file using Node.js? function foo() { console.log( this.bar ); } var bar = "global"; foo(); In Chr ...

When navigating back to tab 3 in an Android fragment tabbed layout, the listview mysteriously vanishes

After selecting tab 3, the listview appears. However, if I go back to tab 1 and then select tab 3 again, the listview doesn't show up. Any idea why this is happening? https://github.com/jdavey1996/News-Android-App import android.support.design.widget ...