我创建了带有两个页面的离子应用程序,如果我导航到Page2,则从Page1开始 . 按下硬件后退按钮导航回到Page1,再次硬件返回应用程序转到后台 . 如果从后台打开应用程序,则会重新加载启动画面并重新启动应用程序 .
如何停止重新启动应用程序?
我使用插件https://ionicframework.com/docs/native/app-minimize/来最小化应用程序
在app.component.ts中
从'@ ionic-native / app-minimize'导入;从'ionic-angular'导入{App,Platform};
构造函数(platform:Platform,public appMinimize:AppMinimize,public app:App){platform.ready() . then(()=> {platform.registerBackButtonAction(()=> {
let nav = app.getActiveNavs()[0]; let activeView = nav.getActive(); if (activeView.name === "Page1") { /** minimize the app in background **/ this.appMinimize.minimize(); } else { nav.pop(); } }); });
}
您可以显示警报以确认退出应用 . 按以下方式修改 app.component.ts 文件,
import { Platform, IonicApp } from 'ionic-angular'; constructor(public platform: Platform, private ionicApp: IonicApp){} initializeApp() { this.platform.ready().then(() => { //back button handle this.platform.registerBackButtonAction(() => { if (this.navctrl.canGoBack()) { this.navctrl.pop(); } else { this.showAlert(); } }); }); } showAlert() { let confirm = this.alertCtrl.create({ title: 'Exit Application?', message: 'Do you want to exit this application?', buttons: [ { text: 'No', handler: () => {} }, { text: 'Yes', handler: () => { navigator['app'].exitApp(); } } ] }); confirm.present(); }
或者您只需按Home键而不是后退键 . 所以它不会重新启动你的应用程序 .
好吧,你在评论中解释自己也许这会有所帮助吗?
请按照以下步骤操作:
STEP ONE: 在您的页面中注册一个功能,如您想要执行CUSTOM ON PAUSES的操作!
STEP TWO: 在那个自定义方法中注册一个订阅者并打开暂停侦听器,你可以完成你的功能
让我们假设您有两个字段变量用于保存订阅者事件,例如
sub1$:any sub2$:any
在构造函数中,你可以做这样的事情!
this.platform.ready().then(() => { this.platform.registerBackButtonAction(() => { this.sub1$=this.platform.pause.subscribe(() => { console.log('****YOURPAGE PAUSED****'); this.appMinimize.minimize(); }); }); });
注意!!暂停事件可以被多次调用,所以你应该不认识太多!而像这样离开页面
ionViewWillUnload() { this.sub1$.unsubscribe(); this.sub2$.unsubscribe(); }
这个帖子已经有几个月了,但是我也可以添加这个解决方案 .
在 app.module.ts 中,将此添加(或修改)到您的导入:
IonicModule.forRoot(MyApp, { navExitApp: false}),
当您位于导航堆栈的根页面时,这将阻止您的应用关闭 .
但请注意,如果您想将应用程序推送到后台,您仍然需要以其他方式处理该应用程序 . 此解决方案仅阻止您的应用关闭 .
4 回答
我使用插件https://ionicframework.com/docs/native/app-minimize/来最小化应用程序
在app.component.ts中
从'@ ionic-native / app-minimize'导入;从'ionic-angular'导入{App,Platform};
构造函数(platform:Platform,public appMinimize:AppMinimize,public app:App){platform.ready() . then(()=> {platform.registerBackButtonAction(()=> {
}
您可以显示警报以确认退出应用 . 按以下方式修改 app.component.ts 文件,
或者您只需按Home键而不是后退键 . 所以它不会重新启动你的应用程序 .
好吧,你在评论中解释自己也许这会有所帮助吗?
请按照以下步骤操作:
STEP ONE: 在您的页面中注册一个功能,如您想要执行CUSTOM ON PAUSES的操作!
STEP TWO: 在那个自定义方法中注册一个订阅者并打开暂停侦听器,你可以完成你的功能
让我们假设您有两个字段变量用于保存订阅者事件,例如
在构造函数中,你可以做这样的事情!
注意!!暂停事件可以被多次调用,所以你应该不认识太多!而像这样离开页面
这个帖子已经有几个月了,但是我也可以添加这个解决方案 .
在 app.module.ts 中,将此添加(或修改)到您的导入:
当您位于导航堆栈的根页面时,这将阻止您的应用关闭 .
但请注意,如果您想将应用程序推送到后台,您仍然需要以其他方式处理该应用程序 . 此解决方案仅阻止您的应用关闭 .