Back button problem: Clear the login view from history

After strungling a long time, i still could not prevent the user from going to login page once they are already logged in

given here my desired state flow:
login → dashboard —> … —> account (sign out) —> login

//in loginViewController:
$location.path(’/dashboard’).replace();
$state.go(“dashboard”)
//does not work

By pressing the physical back button serveral times, users can go to login page with out signing out from account page. I also don’t want the users to be stuck by redirection. It should remove the login view from the history stack.

Continuing the discussion from Remove page from history, so “back” will work properly:

Hi @haibtdt,

In your loginViewController, right before the redirect to your dashboard, add the following code:

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

That will clear the current view (login) from the history. When the user is on the dashboard, and they hit the back button, the app will close since the dashboard is now the only item in the view history.

Add the same code to your account sign out before the redirect to the login view. This way after the user is signed out, hitting the back button from the log in screen will only close the app.

Let me know if any of this doesn’t make sense.

Regards,

5 Likes

It works great. Thank you very much for a very clear instruction. Btw, where can I learn more about that service? And is that the only way?

You can add a nav-clear to your UI element, but I find it better to do it in code.

Unfortunately, I don’t have more documentation on the subject.

UPDATE: So apparently $ionicViewService.nextViewOptions is deprecated so just replace it with $ionicHistory.nextViewOptions

1 Like

Hi. I tried with putting the above code before redirecting to dashboard and it is working as expected. But once I am on dashboard, click log out from side menu, and redirect to login view again, the back button does nothing. Could you please suggest how to make the back button work with logging out process? Thanks.