Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49240

Subscribe to hardware backbutton on android does not work anymore

$
0
0

@shorstmann wrote:

Hi,

subscribing to the hardware backbutton seems to not work anymore after i used “npm update” inside my procect. Even the first log that the button was pressed will not be shown in the console.

in my app.component.ts after deviceready i subscribe to it like the following:

this.platform.ready().then(() => {
..some code before
// active hardware back button
        this.backButtonEvent();
});

The function i use for backButton to handle different events

// active hardware back button
    backButtonEvent() {
        console.log('activate backButton...');
        this.platform.backButton.subscribe(async () => {
            console.log('BackButton pressed');

            // close action sheet
            try {
                const element = await this.actionSheetCtrl.getTop();
                if (element) {
                    console.log('actionSheet close');
                    element.dismiss();
                    return;
                }
            } catch (error) {
            }

            // close popover
            try {
                const element = await this.popoverCtrl.getTop();
                if (element) {
                    console.log('popoverCtrl close');
                    element.dismiss();
                    return;
                }
            } catch (error) {
            }

            // close modal
            try {
                const element = await this.modalCtrl.getTop();
                console.log('ChangePasswordModalIsClosable:', this.global.isChangePasswordModalClosable());


                    //wenn true darf jeder Modal geschlossen werden
                    if (element) {
                        if (this.global.isChangePasswordModalClosable()) {
                            console.log('modalCtrl close');
                            element.dismiss({cancelledModal: true});
                            return;
                        }
                        else{
                            console.log('modalCtrl no close');
                            return ;
                        }
                    }
            } catch (error) {
                console.log(error);
            }

            // close side menu
            try {
                const element = await this.menu.getOpen();
                if (element) {
                    console.log('sideMenu close');
                    this.menu.close();
                    return;
                }
            } catch (error) {
            }

            this.routerOutlets.forEach((outlet: IonRouterOutlet) => {
                console.log("routerOutlets");
                console.log(this.router.url);
                if (outlet && outlet.canGoBack()) {
                    console.log('outlet pop');
                    outlet.pop();

                } else if (this.router.url === '/menu/tabs/dashboard' || this.router.url === '/login') {
                    if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
                        navigator['app'].exitApp(); // work in ionic 4
                    } else {
                        this.alertProvider.presentToast('PRESS_AGAIN_TO_EXIT_APP');
                        this.lastTimeBackPress = new Date().getTime();
                    }
                } else {
                    console.log("routerOutlets else")
                    this.navController.pop();
                }
            });
        });
    }

Have there been any changes?

Posts: 2

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49240

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>