title: How to go to the previous Tab when the Device Back Button is pressed in Flutter?
body: <p>Let's say I click on the tab "Page 2" and then "Page 3". Now when I click the back button on the device I want to go back to "Page 2". </p>
  
  <p>What happens for me is that the app closes. Am I using the wrong Widget or what should I do to ensure when the user uses the back button on the device it goes to the previous tab and not force close the app.</p>
  
  <pre><code>class MovieRouting extends StatefulWidget {
    @override
    MovieRoutingState createState() =&gt; new MovieRoutingState();
  }
  
  // SingleTickerProviderStateMixin is used for animation
  class MovieRoutingState extends State&lt;MovieRouting&gt; with SingleTickerProviderStateMixin {
    int i = 0;
    var pages = [new Page1(), new Page2(), new Page3(), new Page4()];   
  
    @override
    void initState() {
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {  
  
      return new Scaffold(
        body: pages[i],
        // drawer: new AppNavigationDrawer(),
        bottomNavigationBar: new BottomNavigationBar(
          items: [
            new BottomNavigationBarItem(
              icon: new Icon(Icons.home),
              title: new Text('page 1'),
            ),
            new BottomNavigationBarItem(
              icon: new Icon(Icons.photo_library),
              title: new Text('page 2'),
            ),
            new BottomNavigationBarItem(
              icon: new Icon(Icons.book),
              title: new Text('page 3'),
            ),
            new BottomNavigationBarItem(
              icon: new Icon(Icons.notifications),
              title: new Text('page 4'),
            ),
          ],
          currentIndex: i,
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            print (index);
            setState(() {
              i = index;
            });
          },
        ),
      );
  
    }
  }
  </code></pre>