registerBackButtonAction in a controller blocks the back button in whole app

Hi,

I have used registerBackButtonAction in one controller, so it should be called just in that view using that controller.
But it happens that the back button is like disabled and to make it work i should implement the behavior in each controller related to a view.
Also i would like to link a animation like ā€œnav-direction=backā€ used in a view, is it possible?

thanks

You can put the registerBackButtonAction in the appā€™s run function and then check the state that you are in to only override it on that state:

app.run(function($ionicPlatform, $state) {
    $ionicPlatform.registerBackButtonAction(function () {
        if ($state.current.name == "stateToOverride") {
            // override the back button for this state
        // Clear the history before navigating back since these are top level menu items
        } else {
            navigator.app.backHistory();
        }
    }, 100);
});

I donā€™t understand your second question. Can you explain what you mean?

1 Like

Thanks!
I was thinking to put it in the appā€™s run function, mine was more an idea to put it into the controller to see if it works just in that case :).

I try to explain better my other question:
When i click the back button in a device the view comes from left to right instead of from right to left, so i would like to control this animation, i hope i explained myself

Also i have another doubt:
I have this navigation path with different states:
A -> B -> C -> D
When i press the back button on my device works fine going back forward:
A <- B <- C <- D
but when i am in the state D, i have a ng-click that calls a service and then i call the history back:

<i nav-direction="back" ng-click="insert()"  class="button button-small icon icon-paper-plane pull-right"></i>

in My controller:

$scope.insert = function(){
          Service.addItem(item).then(function(data){
			$ionicHistory.goBack();
		});
	}

}

Following this path it happens:
D->C->D->C->B->A
So from D goes to C and then back to D.
I hope its clear :smiley:

You can add $ionicViewSwitcher.nextDirection('forward'); right before you call $ionicHistory.goBack().

Am I understanding correctly? :smile:

Yes!
that is the solution!
But :smiley: now i have another weird behavior:
I have used one code pen and changed a bit: http://codepen.io/anon/pen/RNOJRN
From the home you go to page B then page C, then go back to page B and then again to page C, in this moment if i decide to go back with the back button of my device it goes to page B then C then B again and finally A, the behavior that i expect is that should go directly to A

:slight_smile:

Try using $ionicHistory.goBack() instead of navigator.app.backHistory()?

I donā€™t know if this will work, but it sounds good in theory.

Yes i have tried that but i get still the same behavior :slight_smile:

solved!
i have found the solution on this topic:

1 Like

This solved an issue I had! thank you so much :slight_smile: