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.07k stars 13.51k forks source link

[4.0.0-beta.16] Android hardware back button & cancelling camera usage #16505

Closed olivermuc closed 5 years ago

olivermuc commented 5 years ago

Bug Report

Ionic Info

Ionic:

   ionic (Ionic CLI)             : 4.4.0 (/Users/os/.npm-packages/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.0.0-beta.16
   @angular-devkit/build-angular : 0.10.6
   @angular-devkit/schematics    : 7.0.6
   @angular/cli                  : 7.0.6
   @ionic/angular-toolkit        : 1.2.0

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : android 7.1.4, ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.2.5, (and 18 other plugins)

System:

   Android SDK Tools : 26.1.1 (/Users/os/Library/Android/sdk)
   ios-deploy        : 1.9.4
   NodeJS            : v10.13.0 (/usr/local/Cellar/node@10/10.13.0/bin/node)
   npm               : 6.4.1
   OS                : macOS
   Xcode             : Xcode 10.1 Build version 10B61

Describe the Bug Exiting the device's camera feature (made available via cordova plugin) via the hardware back button, results in both closing the camera & popping the nav history stack. However, the router should disregard the back button in case of camera exit.

Steps to Reproduce

  1. build basic app with camera feature (eg bar code reader)
  2. call camera from 2nd page (nav stack should have an entry!)
  3. cancel out of camera by pressing hardware back button
  4. notice that router also navigated to previous page (1st page)

Expected Behavior Cancelling the camera on Android should not interfere with app nav/routing.

Additional Context Suggestion: Perhaps add a nav/router option to temporarily suppress 'can-go-back' via hardward button.

bryce13950 commented 5 years ago

Do you have project files that you could attach, so it's easier to recreate?

olivermuc commented 5 years ago

I'm in-midst of a larger project, unfortunately due to NDA I can't post any code. But the above is generic, ie. I would expect this behavior in any ionic Android app that deals with camera usage. Btw this was already an issue with v3 for which I created a workaround by hijacking the back-button handling. This however is not possible anymore due to the nature of back-button event subscription (vs exclusively 'owning' the handling).

This is the culprit imho:

https://github.com/ionic-team/ionic/blob/e8e71838b34be3bcbb63a944ec12ce1f6e14e07e/angular/src/providers/nav-controller.ts#L33

Edit: The above is triggered regardless of when and where the backbutton was pressed

bryce13950 commented 5 years ago

Well if you could take your app, and remove everything but the bare bones then it would probably go a long way in getting this solved quicker. You have provided very good steps on how to recreate the bug, but someone is still going to have to spend time recreating the bug. If you do have the ability to provide a small project with the flow already implemented then that would be greatly appreciated.

olivermuc commented 5 years ago

https://github.com/olivermuc/android-hw-back-button-test

Here you go @bryce13950
Plain vanilla template app + bar code scanner plugin. Tested it locally and it behaves the same way.

EDIT: Just to reiterate, ionic v3 still has the same issue. The difference being, that with v3 you can take full control of the hardware back button handling, not with v4, due to the code I pointed out above.

olivermuc commented 5 years ago

Just food for thought: What if we protected the goBack() call by a controller variable:

goBack(animated?: boolean) {
  if (this.disableAndroidBackButton) {
    this.setDirection('back', animated);
    return this.location.back();
  } else {
    return null;
  }
}

This relates to: https://github.com/ionic-team/ionic/blob/e8e71838b34be3bcbb63a944ec12ce1f6e14e07e/angular/src/providers/nav-controller.ts#L64

bryce13950 commented 5 years ago

Thanks for the suggestion we will definitely think about it. It seems like it could be a good solution, but we are going to mull it over, and get back to you.

olivermuc commented 5 years ago

@bryce13950 any updates? It's been 22 days and even a temporary workaround such as manually disabling the platform.backButton.subscribeWithPriority(..)routine would help. Otherwise camera usage is significantly impacted. Thx.

bryce13950 commented 5 years ago

I don't think it has been discussed yet. :/

elylucas commented 5 years ago

Hi @olivermuc and @bryce13950, I am investigating this issue and am not able to reproduce it on the latest Ionic (4.0.1) and Ionic Native (5.0). Clicking the back button on Android while the camera plugin is open leaves me at the page I expect when it closes the native view. Would you mind checking again on the latest and let us know? Or if I'm missing something here?

Thanks

bryce13950 commented 5 years ago

@elylucas If you have tested this on the example project that @olivermuc created, and it is working properly, then it is probably working fine now.

liamdebeasi commented 5 years ago

Hi there,

I have tested the example project on Ionic 4.3.0, and tapping the back button leaves the camera view and brings me back to the page I was on before.

I am going to close this issue as it looks like the problem has been resolved. If you are running into any new back button issues, feel free to open a new bug report.

Thanks!

ionitron-bot[bot] commented 5 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.