ionic-team / ionic-framework

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.
https://ionicframework.com
MIT License
51.02k stars 13.51k forks source link

bug: Android hardware back button override not taken into account on app start #22772

Closed FConstantMovework closed 3 years ago

FConstantMovework commented 3 years ago

Bug Report

Ionic version: 5.30.0

Current behavior: On app start, any click on back button causes the app to exit, whitout firing backbutton or ionBackButton events. Annoying in the case of programmaticaly navigating to a page on app launch.

Expected behavior: Nothing should happen if navigation history is empty, or app should navigate back if navigation history is not empty.

Steps to reproduce: Start new app: ionic start BugBackButton tabs --cordova

Paste given code in app.component.js

Run it: ionic cordova run android

Press back button: app exits instead of displaying alert() and navigating back

Related code:

import { Component } from '@angular/core';

import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private router: Router
  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();

      this.platform.backButton.subscribe(() => {
        alert()
      })

      this.router.navigate(['tabs/tab2'])
    });
  }
}

Other information: As soon as user navigates by touching a navigation element or open menu unexpected behaviour stops occuring. It need to be a "physical" touch that occurs, as even trigering a click event on navigation element initiate navigation but doesn't solve the problem.

Ionic info:

Ionic:

   Ionic CLI                     : 6.11.1 (/home/florent/.nvm/versions/node/v14.4.0/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.0.5
   @angular-devkit/build-angular : 0.803.25
   @angular-devkit/schematics    : 8.3.25
   @angular/cli                  : 8.3.25
   @ionic/angular-toolkit        : 2.2.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 9.0.0
   Cordova Plugins   : cordova-plugin-ionic-webview 4.2.0, (and 28 other plugins)

Utility:

   cordova-res (update available: 0.15.2) : 0.12.1
   native-run (update available: 1.3.0)   : 1.0.0

System:

   Android SDK Tools : 26.1.1 (/home/florent/Android/Sdk)
   NodeJS            : v14.4.0 (/home/florent/.nvm/versions/node/v14.4.0/bin/node)
   npm               : 6.14.5
   OS                : Linux 4.15
liamdebeasi commented 3 years ago

Closing in favor of https://github.com/ionic-team/ionic-framework/issues/22773.

ionitron-bot[bot] commented 3 years ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.