My Hardware 'Back Button Action' is not working in Ionic 4

speccy
rate my pc
how to find computer specs windows 10
pc benchmark
pc builder
newegg
can i run it
how to check pc specs windows 7

I am working in my Ionic 4 app and When the user clicks 2 times on the mobile back button, then it should close the app but this is not happening.

This is my app.component.ts:

lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList<IonRouterOutlet>;

constructor(){
this.backButtonEvent();
}

backButtonEvent() {
    document.addEventListener("backbutton", () => { 
      this.routerOutlets.forEach((outlet: IonRouterOutlet) => {
        if (outlet && outlet.canGoBack()) {
            outlet.pop();
        } else if (this.router.url === '/tabs/tab1') {
          if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
            navigator['app'].exitApp(); //Exit from app
            } else {
            this.presentAlertConfirm();
            this.lastTimeBackPress = new Date().getTime();
          }
          // navigator['app'].exitApp(); // work for ionic 4
        }
      });
    });
  }

  async presentAlertConfirm() {
    const alert = await this.alertController.create({
      // header: 'Confirm!',
      message: 'Are you sure you want to exit the app?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
          }
        }, {
          text: 'Close App',
          handler: () => {
            navigator['app'].exitApp();
          }
        }
      ]
    });

    await alert.present();
  }

This is working when I am on front page(Tab1) and when I am on the other tabs it is not working and not going to the front page.

I think the problem is in my (outlet && outlet.canGoBack()) because this is not working. I am using the tab theme and Can I send the route to the main tab when the user is no other tabs and clicks the hardware back button.

I am using Ionic 4 tab theme.

Any help is much appreciated.

In response to @Raghav comment, I would try it like this:

lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList < IonRouterOutlet > ;

constructor(private platform: Platform) {
  this.backButtonEvent();
}

backButtonEvent() {
  this.platform.backButton.subscribeWithPriority(0, () => {
    this.routerOutlets.forEach(async(outlet: IonRouterOutlet) => {
      if (this.router.url != '/tabs/tab1') {
        await this.router.navigate(['/tabs/tab1']);
      } else if (this.router.url === '/tabs/tab1') {
        if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
          this.lastTimeBackPress = new Date().getTime();
          this.presentAlertConfirm();
        } else {
          navigator['app'].exitApp();
        }
      }
    });
  });
}

async presentAlertConfirm() {
  const alert = await this.alertController.create({
    // header: 'Confirm!',
    message: 'Are you sure you want to exit the app?',
    buttons: [{
      text: 'Cancel',
      role: 'cancel',
      cssClass: 'secondary',
      handler: (blah) => {}
    }, {
      text: 'Close App',
      handler: () => {
        navigator['app'].exitApp();
      }
    }]
  });

  await alert.present();
}

Check Your Computer's Specs: CPU, GPU, Motherboard, & RAM, What Hardware Is Inside My PC: How to Find Graphics, CPU & RAM Specs. What processor, graphics card and RAM is inside your PC? Here's� What Hardware Is Inside My PC: How to Find Graphics, CPU & RAM Specs What processor, graphics card and RAM is inside your PC? Here's how to check your computer's specification.

Try This:

lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList < IonRouterOutlet > ;
backButtonEvent() {
  document.addEventListener("backbutton", async() => {
    try {
      const element = await this.modalCtrl.getTop();
      if (element) {
        element.dismiss();
        return;
      }
    } catch (error) {
      console.log(error);
    }
    this.routerOutlets.forEach(async(outlet: IonRouterOutlet) => {
      if (this.router.url != '/tabs/tab1') {
        await this.router.navigate(['/tabs/tab1']);
      } else if (this.router.url === '/tabs/tab1') {
        if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
          await this.presentAlertConfirm();
          this.lastTimeBackPress = new Date().getTime();
        }
        navigator['app'].exitApp(); // work for ionic 4
      }
    });
  });
}

What Hardware Is Inside My PC: How to Find Graphics, CPU & RAM , How to find computer hardware specs using Settings. You can use the Settings app to check basic hardware and software specs, such as� Test My Hardware - Free PC Hardware Testing Tools. Test My Hardware - This free software is a simple system information and diagnostics utility that provides detailed information about all your hardware component, such as your CPU, memory, operating system, power source.

Do it this Way.

constructor(private platform: Platform) {
  this.platform.backButton.subscribe(() => {

  });
}

How to check your PC's full specifications on Windows 10, Sometimes it becomes essential to see what hardware is installed in your computer or laptop. We present a few free tools which can be used to� Network hardware isn't as clearly defined as some other types of computer hardware. For example, many home routers will often act as a combination router, switch, and firewall. In addition to all the items listed above, there's more computer hardware called auxiliary hardware , of which a computer might have none, or several, of some kinds:

That is because you are calling the registerBackButtonAction before platform is ready. You have to subscribe to the backbutton after the platform is ready. An approaching:

this.platform.ready().then(
  () => {
    this.platform.registerBackButtonAction(() => {
      this.platform.exitApp();
   });
  }
);

PCGameBenchmark: What Can I Run � System Requirements Test, Speccy is the place to start if you need to know what's inside your PC. See a quick summary, or dive deeper into every aspect of your PCs hardware so you can� Whether it's to properly install and run a graphically intensive software program or to troubleshoot your PC, you need to know the hardware components that your computer is using. Windows 8.1 retains the Device Manager tool found in previous versions of Windows, letting you view all installed hardware components -- including your graphics card

try this in your app.component.ts

  lastTimeBackPress = 0;
  timePeriodToExit = 2000;
 @ViewChild(IonRouterOutlet, { static: false }) routerOutlets: IonRouterOutlet

constractor( private router: Router, private alertController: AlertController){this.backbutton()}


backbutton() {
console.log('backbutton')
document.addEventListener("backbutton", () => {
  console.log('backbutton1')
  if (this.routerOutlets && this.routerOutlets.canGoBack()) {
    this.routerOutlets.pop();
  }
  // else if (this.router.url != '/tabs/tabs/tab1') {
  //   this.router.navigate(['/tabs/tabs/tab1']);
  // } 
  else if (this.router.url === '/home') {
    if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
      this.lastTimeBackPress = new Date().getTime();
      this.presentAlertConfirm();
    } else {
      navigator['app'].exitApp();
    }
  }
});
  }



 async presentAlertConfirm() {
const alert = await this.alertController.create({
  // header: 'Confirm!',
  message: 'Are you sure you want to exit the app?',
  buttons: [{
    text: 'Cancel',
    role: 'cancel',
    cssClass: 'secondary',
    handler: (blah) => { }
  }, {
    text: 'Close App',
    handler: () => {
      navigator['app'].exitApp();
    }
  }]
});
await alert.present();
  }

10 Tools To Check Every Hardware Detail Of Your Computer In , Ever had problems with a certain piece of hardware and the tech asked you, “ What Want to record your computer's hardware specs quickly and easily? My name is Aseem Kishore and I am a full-time professional blogger. Use our interactive store locator to search our 5,000+ locally owned stores to easily find the one nearest you.

Download Speccy, Q: The Advisor has installed and finished a discovery of my PC's hardware and software, but I do not see any results. This usually happens when the Windows� Hardware Supply sells Electrical, Plumbing, Pipe, Fixtures, Fittings, Glue, Tools, Tape, and other Home Improvement and small business supplies at very low prices.

Easy Ways To Find Hardware & Other Computer Specifications, Check out our guide on how to find your PCs hardware specs. one of my favorite apps for getting system specs on Windows is aptly called:�

Products: Belarc Advisor,

Comments
  • This may be a duplicate of stackoverflow.com/questions/51728175/…
  • @JayOrdway.I have used that but It is not working. So can you please help me in this?
  • @JayOrdway. Can you help me with the code for closing the app when the user double clicks on back button in Ionic 4?
  • @JayOrdway. Can you help me with the solution?
  • Magic77 seems to have the correct solution below
  • for me this.routerOutlets.forEach(async (outlet: IonRouterOutlet) => {}) is not working.
  • Whats the difference between ViewChildren and ViewChild
  • @AnandRaj. Please refer this: stackoverflow.com/questions/34326745/…
  • If you use this.platform.backButton.subscribeWithPriority(0, () => {...}) you can skip all the manual closing of modals, see here: stackoverflow.com/a/51728578/2422125 Also using document.addEventListener("backbutton") directly is not recommended
  • @FabianN.Okay, so what you suggest for this?
  • In my Case back button reloads the application not exiting.
  • Showng Error: Property 'registerBackButtonAction' does not exist on type 'Platform'.
  • This is not ionic 4
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. Further explanation would help people understand why your solution is good one and how it works.