Remove page from history, so "back" will work properly


#1

I have my app that you need to login to get in to the other pages.

so the first page is “login” and it checks if you are already logged, if so you will be redirected to the main page app, if not it will show you the login page.

now the problem is when the user is inside the logged page area, and he clicks back he will get to the “login” page and than redirected back to the main page, as he is logged in already.

So he is stuck in an infinite loop.

how can I remove the login page from the history.

just like in android “android remove activity from history stack”


Back button problem: Clear the login view from history
#2

I’m using this command to clear history:

    $ionicViewService.clearHistory();

#3

this works nice when I’m checking this on the browser,
but when I check the app on my android device, it still takes me back…


#4

finally I found the answer in a small weird page:

 $ionicViewService.nextViewOptions({
         disableBack: true
 });

that’s all!!!


#5

Could show me an example? I don’t know where to put that code, in log-in page or a page after?


#6

I hope I could help…

I have this on the login function:

$scope.login = function () {

    Security.login($scope.cred.email, $scope.cred.password)
        .success(function(data) {
            Security.setUser(data.data[0]);
            $ionicViewService.nextViewOptions({
                disableBack: true
            });
            $state.go('posts', {}, {location: "replace", reload: true});
        }).error(function(data) {
            $scope.showAlert();
        });
};

and in the logout

  $scope.logout = function () {
        //perform logout
        Security.logout();
        $ionicViewService.nextViewOptions({
            disableBack: true
        });
        $state.go('login', {}, {location: "replace", reload: true});
    };

tell me if it worked for you…


#7

Excellent, thank you so much. The problem had already left me in pain for last 3 months. UX now no longer suffers.
Wonder what i am missing when research about angular ui router does not lead me to a solution? Or that’s $ionicViewService fulfills?


#8

I struggled on it for a week, with ui-router etc…

until I found in some weird location this bit of code, and it worked :smile:


#9

great good too know.