Disable browser back or Android device Back button


#1

Hello all.

I have been working with Ionic for a project and have just loved it. The good guys on this forum have helped me through all my queries so far and am really thankful to you all. I have another query which is as follows.

I wish to disallow the user from navigating back to the previous page using browser back button or by using the device back button on an Android device. I have a CodePen here which will showcase the problem.

Link: http://codepen.io/shreerangp/pen/cKutC?editors

Click on the login button on the login page --> You will be take to the home page --> Open the side menu and click on Activity or Send Feedback --> You will be navigated to the respective page --> Now click on Logout and you will be navigated to the Login page --> Click the browser back or the Android device back button --> You will go back to the Activity/Send Feedback page

I want to disallow this. Once the user clicks on logout, the user should not be able to go back to the previous page and should stay on the login page. I searched through the Ionic docs and forums for similar issues and implementations, but did not come across any concrete solution.

Any help is greatly appreciated.

Regards.
Shreerang
Spatial Unlimited


#2

You are using Ionic 0.9.27. If you use the beta, you can use the new nav-clear directive on the logout link. When a link or button is clicked that has nav-clear, the navigation stack is deleted.

If you need to stick with 0.9.27, search the forum for “history” or “clear history”. There are some old posts that had a few solutions.


#3

Hi!

I wasnt able to disable the back button, but I managed to do this whit some routing logic with the $stateChangeStart. If the user isnt logged and he is requesting other pages than the login, I simply disable the navigation:

$rootScope.$on('$stateChangeStart',
	function (event, toState, toParams, fromState, fromParams) {
		if (!isLogged && toState.name != "login") {
			event.preventDefault();
		}
	}
);

Also you can put here other logic also, stop navigation if the app is offline, dont go back to login page if the user is logged, etc…