# Android BackButton issues

**URL:** <https://forum.ionicframework.com/t/android-backbutton-issues/139847>\
**Category:** ionic-v3\
**Created:** [August 27, 2018, 10:43am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847 "2018-08-27T10:43:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 10:43am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/1 "2018-08-27T10:43:51Z")

</div>

Hello!

I’m having a weird issue. I’ve taken over an Ionic app which I will continue developing and fixing bugs. One bug I have is preventing the app from just closing down when clicking the backbutton.

I’ve tried and I didn’t get it to work. Although, with the same code, I tried making fresh Ionic project, made the same thing, and then it worked.

All the || || in the if the statements were just for testing. I would appreciate if you had any idea why thisi is happening. On a very tight schedule right now.

```auto
import { Component } from '@angular/core';
import { Platform,Events, App, AlertController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Network } from '@ionic-native/network';
import { NetworkProvider } from '../providers/network/network';
//Pages
import { LoginPage } from '../pages/login/login';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  private debug: boolean

  rootPage:any = LoginPage;

  constructor(
    platform: Platform, 
    statusBar: StatusBar, 
    splashScreen: SplashScreen, 
    public events: Events,
    public network: Network,
    public networkProvider: NetworkProvider,
    app: App,
    private alertCtrl: AlertController
  ) 
    {

      this.debug = true

      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.
        statusBar.styleDefault();
        splashScreen.hide();

        this.networkProvider.initializeNetworkEvents();
        // Offline event
        this.events.subscribe('network:offline', () => {
            alert('network:offline ==> '+this.network.type);    
        });

        // Online event
        this.events.subscribe('network:online', () => {
            alert('network:online ==> '+this.network.type);        
        });
    
      });

//BACKBUTTON EVENT HERE
      platform.registerBackButtonAction(() => {
 
        let nav = app.getActiveNavs()[0];
        let activeView = nav.getActive();            
        
        if(this.debug === true){
          console.log('APP.COMPONENT.TS: registerBackButtonActivity(): avtiveView: ', activeView.name)
        }

        if(activeView.name === "HomePage" || activeView.name === "GroupPage" || activeView.name === "LoginPage" || activeView.name === "MyApp") {
     
            if (nav.canGoBack()){ //Can we go back?
                nav.pop();
            } else {
                const alert = this.alertCtrl.create({
                    title: 'App termination',
                    message: 'Do you want to close the app?',
                    buttons: [{
                        text: 'Cancel',
                        role: 'cancel',
                        handler: () => {
                            console.log('Application exit prevented!');
                        }
                    },{
                        text: 'Close App',
                        handler: () => {
                            platform.exitApp(); // Close this application
                        }
                    }]
                });
                alert.present();
            }
        }
      });

  }
}

```

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 10:57am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/2 "2018-08-27T10:57:48Z")

</div>

> [@danielcombain](#):
>
> if(activeView.name === “HomePage” || activeView.name === “GroupPage” || activeView.name === “LoginPage” || activeView.name === “MyApp”) {

Spontaneously I would say that you could not rely on page name while using lazy loading, you might found I think some other post about this on the forum

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 11:17am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/3 "2018-08-27T11:17:22Z")

</div>

Alright, will check it out 🙂

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 11:18am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/4 "2018-08-27T11:18:32Z")

</div>

But in that case, why did it work in my fresh project where I didn’t do anything else with pagehandling, it just worked out of the box?

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 11:23am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/5 "2018-08-27T11:23:56Z")

</div>

A fresh v3 project comes without lazy loading I guess

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 11:24am UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/6 "2018-08-27T11:24:23Z")

</div>

Here I’m assuming you are using lazy loading in the project where it doesn’t work, right?

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 4:05pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/7 "2018-08-27T16:05:17Z")

</div>

Yeah, the workflow is rootPage: LoginPage. User is logging in, pushing “GroupPage” which is a tabspage.

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 4:20pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/8 "2018-08-27T16:20:58Z")

</div>

You could maybe give a try with

```
const activeView: ViewController = this.navController.getActive();

```

and then have a look in `activeView` if you see something to identify the page (like the instance)

Hope it could help

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 4:35pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/9 "2018-08-27T16:35:39Z")

</div>

Alright, cool, will give it a try. I did make it for each and every page to get the name, and it says correctly “LoginPage”, “HomePage” and so on, but will try your way.

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 7:01pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/10 "2018-08-27T19:01:38Z")

</div>

Ok so just to test I did what you wrote and yes, I get data, but it’s the same data I saw before. What I did now though was add an else to the backbuttonAction, just to print an alert. And it’s not even going in there, the problem must be somewhere else. It’s not hooking the backbutton if you understand what I mean?

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 7:09pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/11 "2018-08-27T19:09:37Z")

</div>

I also tried setting an alert before any code executes and nothing even happens. So yeah, the app doesn’t bind to this function at all which is weird since it is doing it with the exact same code, at the same place in a fresh project. Ripping my hair off 😛

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 7:15pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/12 "2018-08-27T19:15:05Z")

</div>

Ionic v3 right? Not v4 right?

Here’s a (working) copy/paste of my Ionic v3 app:

```
private overrideHardwareBackAction() {
    this.platform.ready().then(() => {
        this.unregisterCustomBackActionFunction = this.platform.registerBackButtonAction(() => {
            let activeView: ViewController = this.navController.getActive();

            if (activeView != null && ((<any> activeView).instance instanceof MyPage)) {
                this.backToPreviousSlide();
            } else {
                this.navController.pop();
            }
        });
    });
}

```

look the same as, except maybe the platform ready

P.S.: Yes I know, instance of is ugly, I removed it when I migrated to v4 😉

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 7:16pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/13 "2018-08-27T19:16:27Z")

</div>

Yikes, actually, 4.11 wrong forum? \>.\>

Will test it. Thanks!

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 7:19pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/14 "2018-08-27T19:19:35Z")

</div>

That’s change everything!

wait

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 7:20pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/15 "2018-08-27T19:20:08Z")

</div>

wait 4.11?

I don’t speak about the ionic cli I mean ionic angular

could you display your ionic version plz

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 7:23pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/16 "2018-08-27T19:23:27Z")

</div>

Ahh, 3.9.2, sorry (Post must be at least 20 characters, ignore this)

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 7:25pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/17 "2018-08-27T19:25:14Z")

</div>

😉

so the code I displayed above works in v3 for me

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 7:26pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/18 "2018-08-27T19:26:29Z")

</div>

It’s so weird. I tried making a simple function as an alert. It’s not hooking to that either. I mean, in the fresh one, just a simple registerbackbutton…alert… works. Same code doesn’t work in my project. I can’t see anywhere in the code where they override the backbutton anywhere else. Could anything in the config file or any import collide with it?

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [August 27, 2018, 7:29pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/19 "2018-08-27T19:29:40Z")

</div>

mmmh to be honest I don’t know I don’t know your project

have you try to only call `this.unregisterCustomBackActionFunction` when the platform is ready?

---

<div class="post-metadata">

**Author:** ![danielcombain](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@danielcombain](https://forum.ionicframework.com/u/danielcombain)\
**Post date:** [August 27, 2018, 7:32pm UTC](https://forum.ionicframework.com/t/android-backbutton-issues/139847/20 "2018-08-27T19:32:06Z")

</div>

Will try! Is that a global functions or something? From where is it called?

[Next page](https://forum.ionicframework.com/t/android-backbutton-issues/139847.md?page=2)
