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

How to handle android back button #2

Closed
elango opened this issue Mar 28, 2020 · 14 comments
Closed

How to handle android back button #2

elango opened this issue Mar 28, 2020 · 14 comments

Comments

@elango
Copy link

elango commented Mar 28, 2020

Hi,

Can you please provide your input on how can we handle the hardware back button when the view is pushed.

Right now when tapping the hardware back button closes the app.

Thanks.

@jefferyleo
Copy link

Yes, this is the question I wanted to ask too. I don't think this support the android back button navigation yet, anyhow see how does @BilalShahid13 reply on this.

@BilalShahid13
Copy link
Owner

Hi,

There is no perfect solution at the moment as android doesn't allow us to override the back button. There is a work around though and you can use the flutter package back_button_interceptor to solve your problem. It works most of the time but not all the time. The best solution would be to enable the platformSpecific property.

This is the reason I had to add the platformSpecific option because persistent bottom navigation is not consistent with the Android design guidelines.

@BilalShahid13
Copy link
Owner

BilalShahid13 commented Apr 6, 2020

Hi,

Can you please provide your input on how can we handle the hardware back button when the view is pushed.

Right now when tapping the hardware back button closes the app.

Thanks.

If you want to just prevent the unintended closure of the app, I would suggest using onWillPop. I will incorporate this in an update soon hopefully.

Offical Documentation: https://api.flutter.dev/flutter/widgets/WillPopScope-class.html

Wrap your widget inside WillPopScope and return a false Future in the onWillPop property

Example:

@override
Widget build(BuildContext context) {
  return WillPopScope(
    child: Scaffold(...),
    onWillPop: () => showDialog<bool>(
      context: context,
      builder: (c) => AlertDialog(
        title: Text('Warning'),
        content: Text('Do you really want to exit'),
        actions: [
          FlatButton(
            child: Text('Yes'),
            onPressed: () => Navigator.pop(c, true),
          ),
          FlatButton(
            child: Text('No'),
            onPressed: () => Navigator.pop(c, false),
          ),
        ],
      ),
    ),
  );
}

@BilalShahid13
Copy link
Owner

BilalShahid13 commented Apr 6, 2020

I think I should mention it in the Readme as this is a genuine issue with only a workaround available at the moment.

Thank you for pointing it out. I will not close this issue yet so if anyone has a better solution he might share here FingersCrossed :D

@BilalShahid13 BilalShahid13 pinned this issue Apr 6, 2020
@BilalShahid13
Copy link
Owner

The issue where the app closes if android's back button is pressed is fixed in latest version 1.3.0.

@SogoGolf
Copy link

great component thanks! any eta on a back button fix for Android ?

@thivankasarathchandra
Copy link

Now app is not exiting at all

@BilalShahid13
Copy link
Owner

great component thanks! any eta on a back button fix for Android ?

I have fixed it 🎉 New version will be hopefully up tomorrow after l update the sample project and the readme. Stay tuned!

@BilalShahid13
Copy link
Owner

BilalShahid13 commented Apr 28, 2020

Now app is not exiting at all

Noted! Stay tuned for the next version with the fix which I will hopefully release tomorrow.

@BilalShahid13
Copy link
Owner

BilalShahid13 commented Apr 29, 2020

Version 1.4.4 is up. The Android hardware/software back button is now being correctly handled. Check the updated sample project and readme.

I'm closing this issue for now 🎉

@fernando-s97
Copy link

What about changing the first scenario from
///1. If the you are on the first tab with all screens popped of the given tab, the app will close.
to
///1. If the you are on the first tab with all screens popped of the given tab, the app request to press the back button again (or any other way to prevent suddenly leaving the app).

How can I achieve this?

@BilalShahid13
Copy link
Owner

BilalShahid13 commented May 28, 2020

What about changing the first scenario from
///1. If the you are on the first tab with all screens popped of the given tab, the app will close.
to
///1. If the you are on the first tab with all screens popped of the given tab, the app request to press the back button again (or any other way to prevent suddenly leaving the app).

How can I achieve this?

It's not possible at the moment but I'll release an update soon with this feature. Stay tuned!

@fernando-s97
Copy link

Hi!

If I do this, onWillPop isn't executed. Is there a way to make this work?

@override
Widget build(BuildContext context) {
  return WillPopScope(
    child: Scaffold(...),
    onWillPop: () => showDialog<bool>(
      context: context,
      builder: (c) => AlertDialog(
        title: Text('Warning'),
        content: Text('Do you really want to exit'),
        actions: [
          FlatButton(
            child: Text('Yes'),
            onPressed: () => Navigator.pop(c, true),
          ),
          FlatButton(
            child: Text('No'),
            onPressed: () => Navigator.pop(c, false),
          ),
        ],
      ),
    ),
  );
}

@BilalShahid13
Copy link
Owner

@fernando-s97 try to use the onWillPop property of PersistentTabView and see if that works for you. Also please note it will only work on android.

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

No branches or pull requests

6 participants