npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ionic3-android-backbutton

v0.0.15

Published

A better and easier way to control the Android Back Button for Ionic3

Downloads

24

Readme

Ionic 3 Android Back Button Handler

NPM version

Installation

npm i ionic3-android-backbutton

Adding to project

Update your app.module.ts as follows:

import {BrowserModule} from '@angular/platform-browser';
import {ErrorHandler, NgModule} from '@angular/core';
import {IonicApp, IonicErrorHandler, IonicModule} from 'ionic-angular';

import {MyApp} from './app.component';
import {HomePage} from '../pages/home/home';

import {BackbuttonModule} from "ionic3-android-backbutton";

@NgModule({
    declarations: [
        MyApp,
        HomePage
    ],
    imports: [
        BrowserModule,
        IonicModule.forRoot(MyApp),
        BackbuttonModule.forRoot()
    ],
    bootstrap: [IonicApp],
    entryComponents: [
        MyApp,
        HomePage
    ],
    providers: [
        StatusBar,
        SplashScreen,
        {provide: ErrorHandler, useClass: IonicErrorHandler}
    ]
})
export class AppModule {
}

Usage

most cases where this plugin is useful is to prevent the user from exiting the app before a certain condition is fulfilled, this is where the plugin gets useful especially when using the registerBeforeExit() method. See the example below.

these methods act globally and will affect the whole app so you need to unregister them manually after you don't need them.

Example1:

make sure user sees an alert before he leaves the app

import {Component} from '@angular/core';
import {AlertController, Platform} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';

import {HomePage} from '../pages/home/home';
import {Backbutton} from "ionic3-android-backbutton";

@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    rootPage: any = HomePage;

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public bb: Backbutton, public alertCtrl: AlertController) {
        platform.ready().then(() => {
            statusBar.styleDefault();
            splashScreen.hide();
            this.bb.registerBeforeExit(() => {
                    let alert = this.alertCtrl.create({
                        title: 'You are now going to exit the app',
                        buttons: ['OK']
                    });
                    alert.present();
                    alert.onDidDismiss(() => {
                        this.bb.exit()
                    });
                    return false;
            })
        });
    }
}

Example 2:

make user press back button a few times before being able to leave the page.

import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {Backbutton} from "ionic3-android-backbutton";

@IonicPage()
@Component({
    selector: 'page-article',
    templateUrl: 'article.html',
})
export class ArticlePage {

    bbcounter = 5;

    constructor(public navCtrl: NavController, public navParams: NavParams, public bb: Backbutton, public toastCtrl: ToastController) {
    }

    ionViewDidEnter() {
        this.bb.registerDefaultAction(() => {
            this.bbcounter --;
            if(this.bbcounter > 0){
                this.toastCtrl.create({
                    message: 'Press back '+this.bbcounter+' more times',
                    duration: 3000,
                    position: 'bottom'
                }).present();
                return false; //cancel backbutton action
            } else {
                this.bb.unregisterAction('default'); //unregistering function because we won't need it anymore
                return true; //allow backbutton action
            }
        })
    }

}

Methods

registerDefaultAction(f: Function)

register the function to be called whenever the back button is pressed. If your function returns true, the default back button behavior with be triggered, if your function returns false the default behavior will be canceled.

registerBeforeExit(f: Function)

register the function to be called whenever the app is about to exit upon hardware back button press. If your function returns true, the default back button behavior with be triggered and your app will exit, if your function returns false the default behavior will be canceled.

unregisterAction(type: string){

| type | Description | | -------------- | --------------------------------------| | default | unregister registerDefaultAction() | | beforeexit | unregister registerBeforeExit() |

unregisterAll()

reverts back button default behavior.

back()

emulate android back button (including potentially exiting the app).

exit()

exit the app.