How can I programatically close an ionic app? - ionic-framework

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

Related

Is there any way to show splashScreen when app is in the background or switching tabs. In ionic 6?

I use privacy-screen plugin to hide content when app is in the background, but it shows just gray background. I need to set splashScreen instead. Any suggestions?
I tried splashScreen plugin, but it is not working for me.
I don't know if it's the best way but I think that using Capacitor appStateChange from #capacitor/app (documentation) you could show and hide a screen with your logo when app is in background or foreground.
import { App } from '#capacitor/app';
App.addListener('appStateChange', ({ isActive }) => {
console.log('App state changed. Is active?', isActive);
if(!isActive) showLogoScreen();
else hideLogoScreen();
});
I hope it helps :)
For iOS, I did it simply in native code: just find the "applicationWillResignActive" method in AppDelegate and insert a view on top of the window.
For Android, sadly I couldn't find a better solution. The "gray background" you described is likely the implementation of Hide screen in 'Recent Apps List', but allow screenshots

Change platform brigtness is triggered when 'Home' is pressed in iOS 15 Simulator

I'm having an issue after my Simulator was updated to iOS 15 in my upcoming Flutter app. When I press the 'Home' button at the top it causes my app to fire the didChangePlatformBrightness() function twice.
Seriously thinking on remove this check while the app is already open and leave it only at startup. Anyone with the same problem and any tips on how to solve it?
On Android everything works fine, just as expected.
I solved this question by monitoring the App lifecycle state. If the app is in resumed state, then I change the color scheme, otherwise I'll do nothing. It occurs because when you press the Home button, the app state changes. See the example below:
void didChangePlatformBrightness() {
if (appState == AppLifecycleState.resumed) {
//give the user a choice for restarting the app, so the colors will
//all be set correctly
showThemeChangeWarning();
setColorScheme();
super.didChangePlatformBrightness();
}
}

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();
})
}

ionic5 webview app browser events are not sync with android device back button

I am using a website in the ionic-5 web-view app. When I go back to the previous page in the Ionic app, Android device back button is not responding. I am unable to go back to previous page in the Ionic app. Ionic web-view app events are not in sync with Android device back button. Please help me to solve this issue.
You can handle hardware back button using platform
import { Platform } from '#ionic/angular';
this.platform.backButton.subscribeWithPriority(10, () => {
// route to previous page or component
});

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.