Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pressing device back button instantly closes app when navigator’s page stack count > 1 and there is toast displayed using ons.notification.toast() #2388

Closed
icaru12 opened this issue Apr 8, 2018 · 2 comments
Assignees

Comments

@icaru12
Copy link

icaru12 commented Apr 8, 2018

Environment

Onsen UI Version:

  • 2.9.2

Framework:

  • None

Framework binding:

  • None

Additional libraries:

  • None

Platform:

  • Android 7.0 Cordova, Windows 10 Chrome

Encountered problem

Pressing device back button or ESC instantly closes app when navigator’s page stack count > 1 and there is toast displayed using ons.notification.toast().

Expected: Pop the current page from the navigator's page stack instead of closing the app.

How to reproduce

  1. Open Chrome on desktop.
  2. Go to the Onsen UI Playground and choose the Vanilla JS Kitchensink Example.
  3. Find ons.notification.toast in the HTML editor and remove the timeout property, then click Run.
  4. Open browser JS console.
  5. In the Preview, go to Dialogs and click the "Toast" button.
  6. Press ESC, "Could not close the app..." message appears in console. In a real device when we press device back button, the app closes instantly.
@icaru12 icaru12 changed the title Pressing device back button instantly closes app when there is toast displayed using ons.notification.toast() and navigator’s page stack count > 1 Pressing device back button instantly closes app when navigator’s page stack count > 1 and there is toast displayed using ons.notification.toast() Apr 8, 2018
@asialgearoid
Copy link
Contributor

Thanks for the report @frosdqy, this is a good find. I had a quick look, and it could actually be a bit awkward to fix given how we currently handle the back button.

The problem is that ons-toast is created as a child of body, along with ons-navigator. When the toast receives the back button event, it passes it straight to its parent, which is body. When body receives a back button event, it closes the app. What we need is to pass the event to ons-navigator in a sensible way. Right now I can't think of a good way to do that.

@icaru12
Copy link
Author

icaru12 commented Apr 9, 2018

Hi @asialgearoid, currently I'm using this workaround:

ons.notification.toast(options);

var nav = document.querySelector('#appNavigator');
var toasts = document.querySelectorAll('ons-toast');
var lastToast = toasts[toasts.length - 1];
lastToast.onDeviceBackButton = nav.onDeviceBackButton._callback.bind(nav);

Explanation: After calling ons.notification.toast(), find the created ons-toast and set its back-button handler to navigator's back-button handler. But it's not an elegant solution to the problem 😅

Maybe add a new option onDeviceBackButton to ons.notification.toast() method's options parameter? So we can set custom back-button handler for toasts created by the method, like so:

var nav = document.querySelector('#appNavigator');

ons.notification.toast({
  message: 'Test',
  timeout: 2000,
  onDeviceBackButton: nav.onDeviceBackButton._callback.bind(nav)
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants