registerBackButtonAction in a controller blocks the back button in whole app


#1

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


#2

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?


#3

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


#4

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:


#5

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

Am I understanding correctly? :smile:


#6

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:


#7

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

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


#8

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


#9

solved!
i have found the solution on this topic:


#11

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