config.xml文件:
preference:<preference name="AutoHideSplashScreen" value="false" /> 设置splash的隐藏方式,false表示不会自动隐藏,需要在app.component.ts中自己设置隐藏点,true表示会自动隐藏;
FadeSplashScreen 布尔值,默认为true。设置为false,禁止启动画面淡入或淡出效果。
FadeSplashScreenDuration 浮点数,默认为2(秒)。启动画面淡入淡出时间。
ShowSplashScreenSpinner 布尔值,默认为true。设置为false,则隐藏splash-screen spinner。
返回键:
在app.component.ts中:
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform, ToastController, App } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = HomePage;
pages: Array<{title: string, component: any}>;
registerBackButton: boolean = false;
checkPage: boolean = false;
constructor(
public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public toastCtrl: ToastController,
public app: App
) {
this.initializeApp();
//一般只有在路由的底栈,点击返回键提示退出app
this.platform.registerBackButtonAction(()=>{
this.goBackLogic(); //判断当前页面
if(this.checkPage) { //点击安卓的返回键提示是否退出app
this.exitApp();
}else {
this.app.goBack(); //点击安卓的返回键返回上一页
}
})
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: HomePage },
{ title: 'List', component: ListPage }
];
}
//是否退出app
exitApp() {
if(this.registerBackButton) {
this.platform.exitApp();
}else {
this.registerBackButton = true;
this.toastCtrl.create({
message: '再按一次推出应用',
duration: 2000
}).present();
setTimeout(() => {
this.registerBackButton = false;
}, 2000)
}
}
// 判断当前页面, 如果当前页面是HomePage或者ListPage, 那么点击安卓的返回键是提示是否退出app,如果是其它页面,则返回上一页
goBackLogic() {
var currentCmp = this.app.getActiveNav().getActive().component //获得当前页面的component
var isPage1= currentCmp === HomePage
var isPage2= currentCmp === ListPage
if (isPage1 || isPage2 ) {
this.checkPage = true
} else {
this.checkPage = false
}
}
initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
}
这里的逻辑是:当点击安卓的返回按钮,先判断当前页面是否是路由栈的底栈,如果不是,点击返回,应该返回到应用的上一页,如果是底栈,点击返回,弹出提示“再点击一次退出应用”,如果在两秒内点击两次返回键,则退出app。