2
votes

On the tap of hardware back button on a specific view, it should not do anything. By Default it nav pop the view but here I don't want to back navigate on click of hardware back button. I want it to stay on that page/view. I checked all possible solution available but no luck. This is not a duplicate too.

Ionic 2 - Disabling back button for a specific view this is for nav back button. I want a solution for hardware back button.

6
You should never do that. Think a little bit in the user if you want to be an Android developer. - thelawnmowerman
I need to that for the specific view not for all views as per requirement - kamal soni
I am nav back button disabling back button and swipe. Need to do for hardware back button too. - kamal soni
looking for a ionic solution. - kamal soni

6 Answers

1
votes

You can Try this

*Simply Override Empty onBackPressed *

@Override public void onBackPressed(){

// do nothing.

}

1
votes

You can Try this

*Simply Override Empty onBackPressed *

@Override
public void onBackPressed(){
 // do nothing.}
0
votes

In your Activity you have to override method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        return !disableBackKey ;
    }

    return super.onKeyDown(keyCode, event);
}

And make disableBackKey True/False depend upon your requirement.

0
votes

You have to write an override method

@Override
public void onBackPressed() {
    \\you can do whatever you want to here.
    \\don't call **super**, if u want disable back button in current screen.
}
0
votes

If you are using Activity, please add below function to your activity

@Override
public void onBackPressed() {
    //super.onBackPressed();
}

if you are using Fragment, please add below function to your fragment

public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
       return true
    }
            return false;
}
0
votes

Answer for Ionic 2 or 3

Call the below method at the end of constructor.

Registration in the below code is your class name of current page or component.

if you don't want to do anything on back button, simply remove the code in IF condition below.

overrideHardwareBackButton() {
            this.platform.ready().then(() => {       
                this.platform.registerBackButtonAction(() => {
                    let activeView:ViewController = this.navCtrl.getActive();
                    if (activeView != null && ((<any> activeView).instance instanceof Registration)) {
                        console.log("Registration -> Home");
                        this.nav.setRoot(HomePage);
                    } else {
                        console.log("Somthing is wrong");
                    }
                });
            });
        }