document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", backButtonEvent, false);
}
function backButtonEvent() {
var currentUrl=window.location.hash;
if(currentUrl=='#/homePage' || currentUrl=='#/'){
navigator.app.exitApp();
}
else{
history.go(-1);
navigator.app.backhistory();
}
}
Iam using ionic framework and phone gap.. here when i click device backbutton from home page its just minimizing that app not exiting. How can i exit my app??
In case you mean by minimizing that app that the application goes to the background (is not the active application but it is still in the list of running applications). In case that is what you mean, please notice that this is the normal behavior in Android and many Mobile OS.
Terminating (exiting if you prefer) the application is not under the control of the application developer. It is completely managed by the OS (Android in this case). So, what you have is the normal behavior for apps under Mobile OS.
Instead of terminating the application as you would do under old OS (Linux, Windows and alike), you have to manage the lifecycle of the application: pause, resume, ...
Install plugin https://github.com/ZhichengChen/cordova-plugin-android-home.
Implement 2 handlers for plugins.
Use the bellow code to exit the application.
exitApp:function() {
app.exitApp();
}
Related
I need some assistance to make my App working in background.
I created my first App with Cordova 10, jquerymobile and it works fine excepted in backgound.
The idea is to installed the following plugin cordova-plugin-background-mode, but I get some difficulties to understand how.
I installed the plugin and then I added the following
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
document.getElementById('deviceready').classList.add('deviseIsReady');
cordova.plugins.backgroundMode.enable();
if(Pages.checkConnection() == true)
{
Maps.load();
setInterval(Maps.load, 300000);
}
}
In that way, the mobile is ready, the background mode is actived. I upload my App to my Android but my App stop working.
I wonder, if this is would be a solution
$(window).load(function(){
document.addEventListener("offline", Pages.offLine, false);
document.getElementById("refresh").addEventListener("click", Maps.refresh);
document.getElementById("ffield").addEventListener("change", Maps.field);
document.getElementById("fstations").addEventListener("change", Charts.changeStation);
document.getElementById("threshold").addEventListener("change", Pref.threshold);
document.getElementById("ffieldpref").addEventListener("change", Pref.field);
cordova.plugins.backgroundMode.on('EVENT', backgroundEvent);
});
function backgroundEvent(){
if(cordova.plugins.backgroundMode.isActive()){
cordova.plugins.backgroundMode.moveToBackground();
}
else
{
cordova.plugins.backgroundMode.moveToForeground();
}
}
But I do not know when, it will be actived and if it will work with IOS devise.
Some of you have an experience with Cordova and how to setup the background mode with jQuery Mobile? Any examples?
Many thank for your help and suggestion.
Cheers
You cannot use katzer's cordova-plugin-background-mode with Cordova 10. Works fine on 9.0, but that's as high as it goes. If your Builder program allows you a choice of Cordova versions, pick 9. I have been unable to find an adequate substitute for the plugin that works in 10.
Also in Android, check your config.xml or AndroidManifest.xml and make sure there is FOREGROUND_SERVICE permission. This is absolutely required for background operation.
I have a very simple Phonegap application that loads an external website:
<script type="text/javascript" src="cordova.js"></script>
<script>
function onDeviceReady() {
if (navigator.connection.type == Connection.NONE) {
navigator.notification.alert('An internet connection is required to continue');
} else {
window.location = "http://example.com";
}
}
document.addEventListener("deviceready", onDeviceReady, false);
</script>
When I run it it all works properly and loads the external site. If I switch to a different app and then click the icon to run it (in Android) it switches back to the already running application with no issues.
The problem started occurring when I switched from my testing environment to my production one and switched it to go to an https version. Now, when I click on the icon it restarts the application instead of simply switching back to the application already running.
Is there any way to control whether clicking on the icon reloads the application or simply switches back to the already running application?
EDIT:
Okay, I thought the only difference was changing the "http" to "https", but apparently I also did an upgrade of "phonegap". I tried switching it back to "http" and it's still doing the same wrong thing. Is there any way to control this? I'm currently running 6.5.2 and I think the proper functionality was with 6.5.0 .
EDIT 2:
Alright... It seems that it's somewhat random. I have it running the latest and using "https" and it sometimes reloads and sometimes doesn't.
Mobile operating system OS's each have different ways of managing memory in apps. The general consensus is that the app should always "save early/often" in case it's killed by the OS to free up memory, etc.
In this case, there may be a way though. Try out the Android Launch Mode preference:
<preference name="AndroidLaunchMode" value="singleInstance" />
I've never used this, so perhaps try out each mode too.
I am developing an ionic app. I am wondering if there is any event handler for this condition when the app is being stop/close in follow ways:
1) Settings -> Application -> Application Name -> Force stop
2) Swipe to close app.
I would expect something like this. But the best i could found is to cater for app exiting via $ionicPlatform.registerBackButtonAction which does not include the above two scenario.
Also, I am not sure whether this can be done in native platform as well. Please advise on this.
angular.module('app').run(function($ionicPlatform) {
$ionicPlatform.onAppStopOrForceClose(function() {
// Any function here
});
});
ionic.Platform.exitApp(); // stops the app
window.close();
see: http://ionicframework.com/docs/api/utility/ionic.Platform/
if you try in ios: you can't exit an IOS App, Apple do not allow apps to exit programmatically
I am using ionic framework to build my app, which seems working fine as per my requirement.
Now i close my app and reopen again it maintaining the state which is also fine.
Now my question is
1) Does app.js files run each time when my app is open ?
i tried to add alert in app.js , which works only first time
Is this right or wrong ?
2) I want to run a particular function each time when my app get started. Is there is any way to do this ?
Thanks
There is a very large documentation which gets upgraded every time a new major version of Cordova/Phonegap is released. You can find that documentation here: Cordova Documentation 5.0
It describes an event which is called every time your "device is ready". It is called onDeviceReady. To use this event you need an onDeviceReady-EventListener. The documentation for the EventListeners can be found here: Events in Cordova
You can add that EventListener with this command:
document.addEventListener("deviceready", yourCallbackFunction, false);
Like Zain described in the comments, there is a difference between exiting the application and pausing it, there is also another EventListener which gets called when the user pauses the application. It can be attached to your application with:
document.addEventListener("pause", yourCallbackFunction, false);
So you could create a function which gets called when those two listeners are fired like this:
onDeviceReady
document.addEventListener("deviceready", deviceIsReady, false);
function deviceIsReady() {
alert('Your device is ready!');
}
pause
document.addEventListener("pause", onPause, false);
function onPause() {
alert('Your application is paused');
}
Alternatively, if the alert is not called when the application is paused, you could add the Cordova Plugin Console and call the onPause function with this content:
function onPause() {
console.log('Your application is paused');
}
Please let me know, if this solves your question or if you need further assistance.
I like to know how to exit an app in a good way in Android and Ios?
In the helloworld app of cocos2d-js this code is unfortunately omitted:
var closeItem = cc.MenuItemImage.create(
res.CloseNormal_png,
res.CloseSelected_png,
function () {
cc.log("Menu is clicked!");
}, this);
What can be there instead of
cc.log("Menu is clicked!");
?
For Android, you should call CCDirector::sharedDirector()->end(); to end the app. For Apple though, you could call exit(0) but its not really recommended to exit apps and will appear sort of like a crash. Do you need a close button? Most apps don't tend to have them and just keep running until the system or the user kills them.