How can I close an Ionic 5 app from code? - ionic-framework

I can't find the way to close an app in Ionic 5 system. Looks like the way to do it in Ionic 4 is not working for Ionic 5.
Is it possible to do?

closeApp() {
this.platform.backButton.subscribeWithPriority(999999, () => {
navigator['app'].exitApp();
// or trigger any action you want to achieve
}) //Amended missing a closing bracket
}

Related

How can I programatically close an ionic app?

I've tried the two following approaches to close my ionic app but non has worked.
1.
closeApp(){
this.platform.backButton.subscribeWithPriority(999999, () => {
navigator['app'].exitApp();
})
}
In this approach, when I click on the back button, the app is disappeared, but it's in the list of the background running apps. This doesn't help me, because I need the app to be completely closed and the user be forced to reopen the app by clicking on its icon.
2.
closeApp(){
const { App } = Plugins;
App['exitApp'];
}
in this case, nothing happens, i.e., I stay on the page.
Is there any other approach or plugin which I can use?
I am working with capacitor: 3 and testing on android 12.
import { App } from '#capacitor/app';
ExitApp() {
App.exitApp();
}
try this

How to close mobile app programmatically using IONIC 5?

How to close the mobile app programmatically using IONIC 5? Use case: Upon app start, I am displaying a Terms of Use popover and if the user does not accept, I need to close the app. I am using IONIC5+Angular.
Thank you.
V
I figured it out.
While navigator['app'].exitApp() works for Android, it does not work for iOS. Also, according to Apple, the apps should not terminate themselves. For my app, I will just display the "Accept" but. The user will need to close the app manually if they don't want to agree to the Terms of Use.
navigator['app'].exitApp();
**that's work for me **
you also use
#capacitor/app
App.exitApp();
Force exit the app. This should only be used in conjunction with the backButton handler for Android to exit the app when navigation is complete.
Ionic handles this itself so you shouldn’t need to call this if using Ionic.
HTML :-
<ion-button color="danger" (click)="close()" fill="clear" expand="block">Close App </ion-button>
TS :-
import { Platform } from '#ionic/angular';
constructor(private platform: Platform) { }
close(){
this.platform.backButton.subscribe( () => {
navigator['app'].exitApp();
})
}

How to disable Android back button in ionic 3 pwa app

I am working on progressive web app with ionic 3 and angular 6 framework.
Every thing works fine but when I press my browser back button or mobile back button my application get closed.
I tried to search how to do this, I am able to disable browser back button with java script but on mobile app it's not working.
<script>
// window.onbeforeunload = function() { return "Your work will be lost.";
};
$ionicPlatform.registerBackButtonAction(function (event) {
event.preventDefault();
}, 100);
</script>
I tried to register the back button event and lot's more but none of that is worked fine for me.
Can anyone have same issue then please tell me how to resolve this.
Thanks,
Dattatray
You can use Ionic's Platform to override the physical back button on Android.
Add it into app.component.ts file as such :
import { Platform } from 'ionic-angular';
constructor(public plt: Platform) {}
platform.registerBackButtonAction(() => {
//Add your code here
},100);
Ionic Platform Service.

How to send parameters to a tab in ionic 3

im building a application in ionic 3 witch i wont to send data to a tab
i'm following this documentation:
https://ionicframework.com/docs/api/components/tabs/Tabs/
this is an example of what a want:
ionViewDidEnter() {
this.tabRef.select(2, {one, "1"});
}
thanks

iOS on resume not working

I need a view in my app to refresh everytime the app comes back to the foreground. The code below is working perfectly with Android but nothing happens on iOS.
$ionicPlatform.on('resume', function() {
if($state.current.name == "app.listar_aprovacoes"){
// code to refresh scope and view.
};
});
I also tried the following and it also doesn't work with iOS:
document.addEventListener("resume", function() {
var alertPopup = $ionicPopup.alert({
title: 'Foo',
template: 'Bar'
});
}, false);
So could you help me out with a solution to this problem?
Thanks!
EDIT: For some reason, updating the platform using ionic platform update ios didn't update the code for this. I removed the platform and added it again and it started working.
Try using window.addEventListener('resume', callback); as well.
Also make sure you have the Cordova Device Plugin.
You can install it by using:
ionic plugin add cordova-plugin-device
Generally, with the plugin installed, the first ($ionicPlatform.on('resume', ...)) method should work. I tested both on iOS and Android.
For some reason, updating the platform using ionic platform update ios didn't update the code for this. I removed the platform and added it again and it started working.