How to disable hardware back button when using LoadingController

Hi, I developed a solution for this.
I did it by wrapping the LoadingController.

import {Injectable} from "@angular/core";
import {Loading, LoadingController, LoadingOptions, Platform} from "ionic-angular";


@Injectable()
export class LoaderController {

  constructor(
    private loadingCtrl: LoadingController,
    private platform: Platform
  ) {

  }

  create(opts?: LoadingOptions): Loading {
    let subscription;
    let loading = this.loadingCtrl.create(opts);

    loading.willEnter.subscribe(() => {
      subscription = this.platform.registerBackButtonAction(() => {
        console.log('back button pressed');
      }, 10);
    });

    loading.onDidDismiss(() => {
      subscription();
    });

    return loading;
  }
}

Every Loading you create will replace the backButton original behaviour.

Just make sure in your project there is no backButton action with a higher priority (in this case, I set it to 10).

3 Likes