2

I'm making a mobile app and it has a back button in the navbar of the app. The button is used for navigating one level up in the application. Not all levels of the applications are written in the url, some of them are skipped so i cannot rely on the browser back button because not everything is written in the history. So what i want to do is to prevent the default event that happens when you click the back button (even the url change because i have a function that's manually rewriting the url when you click on some parts of the app) and i want the browser's back button to be tied to my custom function. I'm thinking of adding a false state in the history with history.pushstate so when i'm pressing back i'll be going to the false state, but unfortunately you can't have two states with a same name. Do you guys know any solution for this? Thanks

6
  • 3
    I guess messing around with the default browser behaviour would lead to a very bad user experience Commented Jun 4, 2014 at 13:32
  • My point is: the back button on the screen and the android back button should have the same functionality. Commented Jun 4, 2014 at 13:33
  • Don't do this, use $location.search properly throughout your application, so the back button always works as the user expects it to. Changing default/expected functionality never goes down well with end users. Commented Jun 4, 2014 at 13:41
  • What do you mean by "use it properly"? I'm already using $location.path for setting the path of the app, but some states of the app are not written in the url, so i cannot rely on proper back button navigation... Commented Jun 4, 2014 at 13:44
  • If you want both things do the same thing, make your back button behave like the browser back button ($window.history.back();) and have your app properly deep-linked. Your back buttons are under your control. Browser back is not. Commented Jun 4, 2014 at 14:27

3 Answers 3

5

We want to prevent that users can use the back button when logged off or when the token is not valid anymore.

// Prevent to use the back button.
$scope.$on('$locationChangeStart', function(event) {
 if (!$scope.isAuthenticated) {
   event.preventDefault();
 }
});

Where the if is you can implement your url rewrite function. Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

This works for catching the back button but now i cannot set my location manually with $location.path('my/location').
2

try this:

$rootScope.$on("$routeChangeStart", function(event, next, current) {
    if(current && current.params === "example") {
        $location.path("/defaultPage");
    }
});

Comments

0

The following code should do the trick:

var allowNav = false;
var checkNav = false;

$rootScope.$on('$stateChangeSuccess', function (event, toState, toStateParams, fromState, fromStateParams) {

            allowNav = checkNav;
            checkNav = true;

        });

$rootScope.$on('$locationChangeStart', function (event, next, current) {
            // Prevent the browser default action (Going back)
            if (checkNav) {
                if (!allowNav) {
                    event.preventDefault();
                }
                else {
                    allowNav = false;
                }
            }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.