Go to last state not working


#1

Hi all,

I have one abstract page (like master page) and two child pages i.e Page1 and Page2… I want whenever i click on device’s back button then if i am on Page1 then app needs to be exit (Its working fine with following code) … and whenever i am on Page2 then app needs to go back to previous page. i.e Page1.

following is my piece of code :

.state('main', {
    url: '/main',
    abstract: true,
    templateUrl: 'templates/Main.html',
    controller: 'mainController'
  })

  .state('main.Page1', {
    url: '/Page1',
    views: {
      'Page1-main': {
        templateUrl: 'templates/Page1.html',
        controller: 'mmPage1Controller'
      }
    }
  })

  .state('main.Page2', {
    url: '/Page2',
    views: {
      'Page2-main': {
        templateUrl: 'templates/Page2.html'
      }
    }
  })

.controller('mainController',['$scope', '$state', '$rootScope', '$cordovaNetwork', '$ionicPopup', '$ionicPlatform', '$ionicHistory', function ($scope, $state, $rootScope, $cordovaNetwork, $ionicPopup, $ionicPlatform, $ionicHistory) {
  
  $ionicPlatform.registerBackButtonAction(function (event) {
    
   if($state.current.name=="main.Page1"){
      navigator.app.exitApp();
    }
    else {
      $scope._last_State = $ionicHistory.backView().stateName;
      $state.go($scope._last_State, {}, {reload: false});
    }
  }, 100);
}])

Any help would be appreciated…
Thank you.


#2

you could use

http://ionicframework.com/docs/api/service/$ionicHistory

$ionicHistory.goBack() directly


#3

Hi @bengtler,

Thank you for your reply…

Are you 100% sure $ionicHistory.goBack() will work in above case ? I have tried so many things but nothing worked for me… I didn’t find any workaround as well.

Note that with use of $ionicHistory.goBack(), once i clicked back button on Page2 it is showing me that i am on Page1 now (as per current state name) but the screen doesn’t showing me Page1 content… It shows me Page2 content.

Finally i found following two links :

As per the conversation i have come to know that this is bug in ionic… and as per @mhartington

Not necessarily, since we’re dealing with abstract states. Since state B is abstract, the navigation and history management aren’t sure how to handle. Do we swipe back from B to A or do we do nothing since there is no state to swipe back to from C.

This is a bug, but it’s a bug that would require massive changes to ionic’s history and navigations stacks as well as ui-router. This is being working on in V2, but as it stands it’s not something that we could fix ATM.

Right, we think it should go back to A, but the way our codebase stands ATM doesn’t allow for this :disappointed:

This is something that we are working on for V2, so we are aware of this shortcoming. Closing for now as there’s no easy fix.

Is it solved in V2 ? or i can use V1 with some workaround ?

Thank you,