How to deactivate or override the Android “BACK” button, in Flutter?












23















Is there a way to deactivate the Android back button when on a specific page?



class WakeUpApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "Time To Wake Up ?",
home: new WakeUpHome(),
routes: <String, WidgetBuilder>{
'/pageOne': (BuildContext context) => new pageOne(),
'/pageTwo': (BuildContext context) => new pageTwo(),
},
);
}
}


On pageOne I have a button to go to pageTwo:



new FloatingActionButton(
onPressed: () {
Navigator.of(context).pushNamed('/pageTwo');
},
)


My problem is that if I press the Back arrow at the bottom of the android screen, I go back to pageOne. I would like this button to not show up at all.
Ideally, I would like to have no possible way out of this screen unless the user for example keeps his finger pressed on the screen for 5 seconds. (I am trying to write an App for toddlers, and would like only the parents to be able to navigate out of the particular screen).










share|improve this question





























    23















    Is there a way to deactivate the Android back button when on a specific page?



    class WakeUpApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return new MaterialApp(
    title: "Time To Wake Up ?",
    home: new WakeUpHome(),
    routes: <String, WidgetBuilder>{
    '/pageOne': (BuildContext context) => new pageOne(),
    '/pageTwo': (BuildContext context) => new pageTwo(),
    },
    );
    }
    }


    On pageOne I have a button to go to pageTwo:



    new FloatingActionButton(
    onPressed: () {
    Navigator.of(context).pushNamed('/pageTwo');
    },
    )


    My problem is that if I press the Back arrow at the bottom of the android screen, I go back to pageOne. I would like this button to not show up at all.
    Ideally, I would like to have no possible way out of this screen unless the user for example keeps his finger pressed on the screen for 5 seconds. (I am trying to write an App for toddlers, and would like only the parents to be able to navigate out of the particular screen).










    share|improve this question



























      23












      23








      23


      5






      Is there a way to deactivate the Android back button when on a specific page?



      class WakeUpApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      return new MaterialApp(
      title: "Time To Wake Up ?",
      home: new WakeUpHome(),
      routes: <String, WidgetBuilder>{
      '/pageOne': (BuildContext context) => new pageOne(),
      '/pageTwo': (BuildContext context) => new pageTwo(),
      },
      );
      }
      }


      On pageOne I have a button to go to pageTwo:



      new FloatingActionButton(
      onPressed: () {
      Navigator.of(context).pushNamed('/pageTwo');
      },
      )


      My problem is that if I press the Back arrow at the bottom of the android screen, I go back to pageOne. I would like this button to not show up at all.
      Ideally, I would like to have no possible way out of this screen unless the user for example keeps his finger pressed on the screen for 5 seconds. (I am trying to write an App for toddlers, and would like only the parents to be able to navigate out of the particular screen).










      share|improve this question
















      Is there a way to deactivate the Android back button when on a specific page?



      class WakeUpApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      return new MaterialApp(
      title: "Time To Wake Up ?",
      home: new WakeUpHome(),
      routes: <String, WidgetBuilder>{
      '/pageOne': (BuildContext context) => new pageOne(),
      '/pageTwo': (BuildContext context) => new pageTwo(),
      },
      );
      }
      }


      On pageOne I have a button to go to pageTwo:



      new FloatingActionButton(
      onPressed: () {
      Navigator.of(context).pushNamed('/pageTwo');
      },
      )


      My problem is that if I press the Back arrow at the bottom of the android screen, I go back to pageOne. I would like this button to not show up at all.
      Ideally, I would like to have no possible way out of this screen unless the user for example keeps his finger pressed on the screen for 5 seconds. (I am trying to write an App for toddlers, and would like only the parents to be able to navigate out of the particular screen).







      flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 0:23









      MarcG

      11.3k105353




      11.3k105353










      asked Aug 28 '17 at 10:13









      JackpapJackpap

      1,2591111




      1,2591111
























          4 Answers
          4






          active

          oldest

          votes


















          40














          The answer is WillPopScope. It will prevent the page from being popped by the system. You'll still be able to use Navigator.of(context).pop()



          @override
          Widget build(BuildContext context) {
          return new WillPopScope(
          onWillPop: () async => false,
          child: new Scaffold(
          appBar: new AppBar(
          title: new Text("data"),
          leading: new IconButton(
          icon: new Icon(Icons.ac_unit),
          onPressed: () => Navigator.of(context).pop(),
          ),
          ),
          ),
          );
          }





          share|improve this answer

































            2














            I'm posting this here in case anyone out there finds this and wishes they would find a simple example
            https://gist.github.com/b-cancel/0ca372017a25f0c120b14dfca3591aa5



            import 'package:flutter/material.dart';

            import 'dart:async';

            void main() => runApp(new BackButtonOverrideDemoWidget());

            class BackButtonOverrideDemoWidget extends StatefulWidget{
            @override
            _BackButtonOverrideDemoWidgetState createState() => new _BackButtonOverrideDemoWidgetState();
            }

            class _BackButtonOverrideDemoWidgetState extends State<BackButtonOverrideDemoWidget> with WidgetsBindingObserver{

            //-------------------------Test Variable

            bool isBackButtonActivated = false;

            //-------------------------Required For WidgetsBindingObserver

            @override
            void initState() {
            super.initState();
            WidgetsBinding.instance.addObserver(this);
            }

            @override
            void dispose() {
            WidgetsBinding.instance.removeObserver(this);
            super.dispose();
            }

            //-------------------------Function That Triggers when you hit the back key

            @override
            didPopRoute(){
            bool override;
            if(isBackButtonActivated)
            override = false;
            else
            override = true;
            return new Future<bool>.value(override);
            }

            //-------------------------Build Method

            @override
            Widget build(BuildContext context) {
            return new Directionality(
            textDirection: TextDirection.ltr,
            child: new Container(
            color: (isBackButtonActivated) ? Colors.green : Colors.red,
            child: new Center(
            child: new FlatButton(
            color: Colors.white,
            onPressed: () {
            isBackButtonActivated = !isBackButtonActivated;
            setState(() {});
            },
            child: (isBackButtonActivated) ?
            new Text("DeActive the Back Button") : new Text("Activate the Back Button"),
            )
            )
            ),
            );
            }
            }





            share|improve this answer
























            • This is not work

              – Ravindra Bhanderi
              Jun 16 '18 at 9:52













            • I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

              – Bryan Cancel
              Jun 16 '18 at 16:44













            • @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

              – MarcG
              Jan 2 at 23:22



















            0














            You should use the method popAndPushNamed instead of pushNamed. It pops the current route and pushes the desired route.



            That being said, your FAB code should look like this



            new FloatingActionButton(
            onPressed: () {
            Navigator.popAndPushNamed(context, '/pageTwo');
            },
            )





            share|improve this answer



















            • 1





              This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

              – Jackpap
              Aug 28 '17 at 11:57



















            0














            As Rémi Rousselet pointed out, WillPopScope is usually the way to go. However, if you are developing a stateful widget that should react to the back button directly, you may use this:



            https://pub.dartlang.org/packages/back_button_interceptor






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45916658%2fhow-to-deactivate-or-override-the-android-back-button-in-flutter%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              40














              The answer is WillPopScope. It will prevent the page from being popped by the system. You'll still be able to use Navigator.of(context).pop()



              @override
              Widget build(BuildContext context) {
              return new WillPopScope(
              onWillPop: () async => false,
              child: new Scaffold(
              appBar: new AppBar(
              title: new Text("data"),
              leading: new IconButton(
              icon: new Icon(Icons.ac_unit),
              onPressed: () => Navigator.of(context).pop(),
              ),
              ),
              ),
              );
              }





              share|improve this answer






























                40














                The answer is WillPopScope. It will prevent the page from being popped by the system. You'll still be able to use Navigator.of(context).pop()



                @override
                Widget build(BuildContext context) {
                return new WillPopScope(
                onWillPop: () async => false,
                child: new Scaffold(
                appBar: new AppBar(
                title: new Text("data"),
                leading: new IconButton(
                icon: new Icon(Icons.ac_unit),
                onPressed: () => Navigator.of(context).pop(),
                ),
                ),
                ),
                );
                }





                share|improve this answer




























                  40












                  40








                  40







                  The answer is WillPopScope. It will prevent the page from being popped by the system. You'll still be able to use Navigator.of(context).pop()



                  @override
                  Widget build(BuildContext context) {
                  return new WillPopScope(
                  onWillPop: () async => false,
                  child: new Scaffold(
                  appBar: new AppBar(
                  title: new Text("data"),
                  leading: new IconButton(
                  icon: new Icon(Icons.ac_unit),
                  onPressed: () => Navigator.of(context).pop(),
                  ),
                  ),
                  ),
                  );
                  }





                  share|improve this answer















                  The answer is WillPopScope. It will prevent the page from being popped by the system. You'll still be able to use Navigator.of(context).pop()



                  @override
                  Widget build(BuildContext context) {
                  return new WillPopScope(
                  onWillPop: () async => false,
                  child: new Scaffold(
                  appBar: new AppBar(
                  title: new Text("data"),
                  leading: new IconButton(
                  icon: new Icon(Icons.ac_unit),
                  onPressed: () => Navigator.of(context).pop(),
                  ),
                  ),
                  ),
                  );
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 5 at 18:05









                  CopsOnRoad

                  5,26922227




                  5,26922227










                  answered Aug 28 '17 at 11:43









                  Rémi RousseletRémi Rousselet

                  36.5k484110




                  36.5k484110

























                      2














                      I'm posting this here in case anyone out there finds this and wishes they would find a simple example
                      https://gist.github.com/b-cancel/0ca372017a25f0c120b14dfca3591aa5



                      import 'package:flutter/material.dart';

                      import 'dart:async';

                      void main() => runApp(new BackButtonOverrideDemoWidget());

                      class BackButtonOverrideDemoWidget extends StatefulWidget{
                      @override
                      _BackButtonOverrideDemoWidgetState createState() => new _BackButtonOverrideDemoWidgetState();
                      }

                      class _BackButtonOverrideDemoWidgetState extends State<BackButtonOverrideDemoWidget> with WidgetsBindingObserver{

                      //-------------------------Test Variable

                      bool isBackButtonActivated = false;

                      //-------------------------Required For WidgetsBindingObserver

                      @override
                      void initState() {
                      super.initState();
                      WidgetsBinding.instance.addObserver(this);
                      }

                      @override
                      void dispose() {
                      WidgetsBinding.instance.removeObserver(this);
                      super.dispose();
                      }

                      //-------------------------Function That Triggers when you hit the back key

                      @override
                      didPopRoute(){
                      bool override;
                      if(isBackButtonActivated)
                      override = false;
                      else
                      override = true;
                      return new Future<bool>.value(override);
                      }

                      //-------------------------Build Method

                      @override
                      Widget build(BuildContext context) {
                      return new Directionality(
                      textDirection: TextDirection.ltr,
                      child: new Container(
                      color: (isBackButtonActivated) ? Colors.green : Colors.red,
                      child: new Center(
                      child: new FlatButton(
                      color: Colors.white,
                      onPressed: () {
                      isBackButtonActivated = !isBackButtonActivated;
                      setState(() {});
                      },
                      child: (isBackButtonActivated) ?
                      new Text("DeActive the Back Button") : new Text("Activate the Back Button"),
                      )
                      )
                      ),
                      );
                      }
                      }





                      share|improve this answer
























                      • This is not work

                        – Ravindra Bhanderi
                        Jun 16 '18 at 9:52













                      • I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

                        – Bryan Cancel
                        Jun 16 '18 at 16:44













                      • @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

                        – MarcG
                        Jan 2 at 23:22
















                      2














                      I'm posting this here in case anyone out there finds this and wishes they would find a simple example
                      https://gist.github.com/b-cancel/0ca372017a25f0c120b14dfca3591aa5



                      import 'package:flutter/material.dart';

                      import 'dart:async';

                      void main() => runApp(new BackButtonOverrideDemoWidget());

                      class BackButtonOverrideDemoWidget extends StatefulWidget{
                      @override
                      _BackButtonOverrideDemoWidgetState createState() => new _BackButtonOverrideDemoWidgetState();
                      }

                      class _BackButtonOverrideDemoWidgetState extends State<BackButtonOverrideDemoWidget> with WidgetsBindingObserver{

                      //-------------------------Test Variable

                      bool isBackButtonActivated = false;

                      //-------------------------Required For WidgetsBindingObserver

                      @override
                      void initState() {
                      super.initState();
                      WidgetsBinding.instance.addObserver(this);
                      }

                      @override
                      void dispose() {
                      WidgetsBinding.instance.removeObserver(this);
                      super.dispose();
                      }

                      //-------------------------Function That Triggers when you hit the back key

                      @override
                      didPopRoute(){
                      bool override;
                      if(isBackButtonActivated)
                      override = false;
                      else
                      override = true;
                      return new Future<bool>.value(override);
                      }

                      //-------------------------Build Method

                      @override
                      Widget build(BuildContext context) {
                      return new Directionality(
                      textDirection: TextDirection.ltr,
                      child: new Container(
                      color: (isBackButtonActivated) ? Colors.green : Colors.red,
                      child: new Center(
                      child: new FlatButton(
                      color: Colors.white,
                      onPressed: () {
                      isBackButtonActivated = !isBackButtonActivated;
                      setState(() {});
                      },
                      child: (isBackButtonActivated) ?
                      new Text("DeActive the Back Button") : new Text("Activate the Back Button"),
                      )
                      )
                      ),
                      );
                      }
                      }





                      share|improve this answer
























                      • This is not work

                        – Ravindra Bhanderi
                        Jun 16 '18 at 9:52













                      • I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

                        – Bryan Cancel
                        Jun 16 '18 at 16:44













                      • @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

                        – MarcG
                        Jan 2 at 23:22














                      2












                      2








                      2







                      I'm posting this here in case anyone out there finds this and wishes they would find a simple example
                      https://gist.github.com/b-cancel/0ca372017a25f0c120b14dfca3591aa5



                      import 'package:flutter/material.dart';

                      import 'dart:async';

                      void main() => runApp(new BackButtonOverrideDemoWidget());

                      class BackButtonOverrideDemoWidget extends StatefulWidget{
                      @override
                      _BackButtonOverrideDemoWidgetState createState() => new _BackButtonOverrideDemoWidgetState();
                      }

                      class _BackButtonOverrideDemoWidgetState extends State<BackButtonOverrideDemoWidget> with WidgetsBindingObserver{

                      //-------------------------Test Variable

                      bool isBackButtonActivated = false;

                      //-------------------------Required For WidgetsBindingObserver

                      @override
                      void initState() {
                      super.initState();
                      WidgetsBinding.instance.addObserver(this);
                      }

                      @override
                      void dispose() {
                      WidgetsBinding.instance.removeObserver(this);
                      super.dispose();
                      }

                      //-------------------------Function That Triggers when you hit the back key

                      @override
                      didPopRoute(){
                      bool override;
                      if(isBackButtonActivated)
                      override = false;
                      else
                      override = true;
                      return new Future<bool>.value(override);
                      }

                      //-------------------------Build Method

                      @override
                      Widget build(BuildContext context) {
                      return new Directionality(
                      textDirection: TextDirection.ltr,
                      child: new Container(
                      color: (isBackButtonActivated) ? Colors.green : Colors.red,
                      child: new Center(
                      child: new FlatButton(
                      color: Colors.white,
                      onPressed: () {
                      isBackButtonActivated = !isBackButtonActivated;
                      setState(() {});
                      },
                      child: (isBackButtonActivated) ?
                      new Text("DeActive the Back Button") : new Text("Activate the Back Button"),
                      )
                      )
                      ),
                      );
                      }
                      }





                      share|improve this answer













                      I'm posting this here in case anyone out there finds this and wishes they would find a simple example
                      https://gist.github.com/b-cancel/0ca372017a25f0c120b14dfca3591aa5



                      import 'package:flutter/material.dart';

                      import 'dart:async';

                      void main() => runApp(new BackButtonOverrideDemoWidget());

                      class BackButtonOverrideDemoWidget extends StatefulWidget{
                      @override
                      _BackButtonOverrideDemoWidgetState createState() => new _BackButtonOverrideDemoWidgetState();
                      }

                      class _BackButtonOverrideDemoWidgetState extends State<BackButtonOverrideDemoWidget> with WidgetsBindingObserver{

                      //-------------------------Test Variable

                      bool isBackButtonActivated = false;

                      //-------------------------Required For WidgetsBindingObserver

                      @override
                      void initState() {
                      super.initState();
                      WidgetsBinding.instance.addObserver(this);
                      }

                      @override
                      void dispose() {
                      WidgetsBinding.instance.removeObserver(this);
                      super.dispose();
                      }

                      //-------------------------Function That Triggers when you hit the back key

                      @override
                      didPopRoute(){
                      bool override;
                      if(isBackButtonActivated)
                      override = false;
                      else
                      override = true;
                      return new Future<bool>.value(override);
                      }

                      //-------------------------Build Method

                      @override
                      Widget build(BuildContext context) {
                      return new Directionality(
                      textDirection: TextDirection.ltr,
                      child: new Container(
                      color: (isBackButtonActivated) ? Colors.green : Colors.red,
                      child: new Center(
                      child: new FlatButton(
                      color: Colors.white,
                      onPressed: () {
                      isBackButtonActivated = !isBackButtonActivated;
                      setState(() {});
                      },
                      child: (isBackButtonActivated) ?
                      new Text("DeActive the Back Button") : new Text("Activate the Back Button"),
                      )
                      )
                      ),
                      );
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 13 '18 at 21:51









                      Bryan CancelBryan Cancel

                      545




                      545













                      • This is not work

                        – Ravindra Bhanderi
                        Jun 16 '18 at 9:52













                      • I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

                        – Bryan Cancel
                        Jun 16 '18 at 16:44













                      • @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

                        – MarcG
                        Jan 2 at 23:22



















                      • This is not work

                        – Ravindra Bhanderi
                        Jun 16 '18 at 9:52













                      • I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

                        – Bryan Cancel
                        Jun 16 '18 at 16:44













                      • @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

                        – MarcG
                        Jan 2 at 23:22

















                      This is not work

                      – Ravindra Bhanderi
                      Jun 16 '18 at 9:52







                      This is not work

                      – Ravindra Bhanderi
                      Jun 16 '18 at 9:52















                      I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

                      – Bryan Cancel
                      Jun 16 '18 at 16:44







                      I literally used it myself yesterday. Keep in mind that it is only for Android, Apple has no back button. Although could you perhaps tell me what goes wrong so I can repair it, or perhaps it only works on my particular emulator. I haven't updated my flutter in a couple weeks so perhaps that's the problem. Let me know

                      – Bryan Cancel
                      Jun 16 '18 at 16:44















                      @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

                      – MarcG
                      Jan 2 at 23:22





                      @BryanCancel Your answer works only if you don't yet have any pushed routes. See method WidgetsBinding.handlePopRoute(). It notifies the observers in registration order, and stops as soon as it receives true. When there are pushed routes, the navigator returns true first, and then your observer never actually gets called. In other words, your code only works to prevent the application from shutting down when the user clicks the back button, when there are no more routes left.

                      – MarcG
                      Jan 2 at 23:22











                      0














                      You should use the method popAndPushNamed instead of pushNamed. It pops the current route and pushes the desired route.



                      That being said, your FAB code should look like this



                      new FloatingActionButton(
                      onPressed: () {
                      Navigator.popAndPushNamed(context, '/pageTwo');
                      },
                      )





                      share|improve this answer



















                      • 1





                        This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

                        – Jackpap
                        Aug 28 '17 at 11:57
















                      0














                      You should use the method popAndPushNamed instead of pushNamed. It pops the current route and pushes the desired route.



                      That being said, your FAB code should look like this



                      new FloatingActionButton(
                      onPressed: () {
                      Navigator.popAndPushNamed(context, '/pageTwo');
                      },
                      )





                      share|improve this answer



















                      • 1





                        This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

                        – Jackpap
                        Aug 28 '17 at 11:57














                      0












                      0








                      0







                      You should use the method popAndPushNamed instead of pushNamed. It pops the current route and pushes the desired route.



                      That being said, your FAB code should look like this



                      new FloatingActionButton(
                      onPressed: () {
                      Navigator.popAndPushNamed(context, '/pageTwo');
                      },
                      )





                      share|improve this answer













                      You should use the method popAndPushNamed instead of pushNamed. It pops the current route and pushes the desired route.



                      That being said, your FAB code should look like this



                      new FloatingActionButton(
                      onPressed: () {
                      Navigator.popAndPushNamed(context, '/pageTwo');
                      },
                      )






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 28 '17 at 10:41









                      icazevedoicazevedo

                      508




                      508








                      • 1





                        This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

                        – Jackpap
                        Aug 28 '17 at 11:57














                      • 1





                        This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

                        – Jackpap
                        Aug 28 '17 at 11:57








                      1




                      1





                      This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

                      – Jackpap
                      Aug 28 '17 at 11:57





                      This doesn't work for me. I had tried this already, but the Android back arrow still brings me back to page one.

                      – Jackpap
                      Aug 28 '17 at 11:57











                      0














                      As Rémi Rousselet pointed out, WillPopScope is usually the way to go. However, if you are developing a stateful widget that should react to the back button directly, you may use this:



                      https://pub.dartlang.org/packages/back_button_interceptor






                      share|improve this answer




























                        0














                        As Rémi Rousselet pointed out, WillPopScope is usually the way to go. However, if you are developing a stateful widget that should react to the back button directly, you may use this:



                        https://pub.dartlang.org/packages/back_button_interceptor






                        share|improve this answer


























                          0












                          0








                          0







                          As Rémi Rousselet pointed out, WillPopScope is usually the way to go. However, if you are developing a stateful widget that should react to the back button directly, you may use this:



                          https://pub.dartlang.org/packages/back_button_interceptor






                          share|improve this answer













                          As Rémi Rousselet pointed out, WillPopScope is usually the way to go. However, if you are developing a stateful widget that should react to the back button directly, you may use this:



                          https://pub.dartlang.org/packages/back_button_interceptor







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 3 at 0:20









                          MarcGMarcG

                          11.3k105353




                          11.3k105353






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45916658%2fhow-to-deactivate-or-override-the-android-back-button-in-flutter%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              How to fix TextFormField cause rebuild widget in Flutter