Different back button event for diffent views?

when i say back button i mean the actual button on the device.

what i am looking for is that i want a confirm exit on the home view of my app and on other views the normal back in history behavior.

i am using side menu template of ionic

 **on home view:->**

      var deregister = $ionicPlatform.registerBackButtonAction(
        function () {
            console.log("close the popup")
        }, 100
);
//Then when this scope is destroyed, remove the function
$scope.$on('$destroy', deregister)

Store your back button registration in a variable -> destroys the function if the view gets destroyed .
This work but when the controller initially loads but if navigate from home->view2->home again this dosn’t seem to work as the scope has been destroyed

what is the way around this?

Hey,

Are you using the right events? maybe you could use BeforeEnter and/or AfterLeave?

Why don’t you check the current state and manipulate it that way? You can add this to your $ionicPlatform.ready function and it will always act that way for the home state.

$ionicPlatform.registerBackButtonAction(function () {
    if ($state.current.name == "home") {
        alert('close app?');
    } else {
        navigator.app.backHistory();
    }
}, 100);
2 Likes

thanks…i created a factory and used ur code inside it so now i can change the back btn even on the factory call based on the view.

1 Like