ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Hardware back button jumps to previous page even with hidden back button

Open oliversalzburg opened this issue 12 years ago • 12 comments

Even if hide-back-button="true" is set on the ion-view, pressing the hardware back button will still jump to the previous page.

oliversalzburg avatar Aug 26 '14 15:08 oliversalzburg

Hiding the back-button doesn't disable the back navigation. Just the display. To prevent this you should use nav-clear

mhartington avatar Aug 26 '14 18:08 mhartington

I'm not sure if nav-clear applies for me.

When my application starts up, I first direct to a loading screen. When the loading operation is completed, I use $location to navigate to the main application page. Here, I hide the back button, but pressing the hardware back button brings me back to the loading screen.

But, looking at nav-clear I guess I could just use $ionicViewService to disable the back action.

I figured that hiding the back button in the UI wouldn't actually have any effect on the underlying ability to navigate the view hierarchy. It just felt like this problem could be approached with a solution that felt more intuitive, like the usage of hide-back-button already does.

oliversalzburg avatar Aug 27 '14 08:08 oliversalzburg

I had same exact problem in my app. Here is how i solved it: Once on main page register back button action and on view destroy event you need to deregister teh action:

      //add event listener to handle Android hardware back button
      //to close the app while user is on main page and logged in
      //to prevent login controller redirecting back to loading page
      var deregister = $ionicPlatform.registerBackButtonAction(
        function () {
          if($state.is('app.main-page')) {
            ionic.Platform.exitApp();
          }
        }, 101
      );
      $scope.$on('$destroy', deregister);

okonon avatar Aug 27 '14 12:08 okonon

@oliversalzburg , you can disable navigation back programmatically with this.

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

So in your navigation logic where you use $location, use $ionicViewService and disable going back. It's basically the functionality of nav-clear

Does this work for you?

mhartington avatar Aug 28 '14 17:08 mhartington

@mhartington It probably will. I'll give that a try once I'm trough with the current tasks. I also like @softBarbarian's idea. Either way, I see that this isn't really an Ionic issue.

I'd just like to suggest that this could be mentioned in the Navigation section of the JavaScript docs. Unless it already is and I just missed it :)

Thanks for your help.

oliversalzburg avatar Aug 29 '14 08:08 oliversalzburg

Step taken

  1. I created a project using “tabs” template of ionic
  2. Renamed the DashCrtl to HomeCtrl, and renamed few other controller. Problem aroused: When the app is installed in android device, and opened, I see a problem. Step taken in android device From “Home” tab, navigated to “Call Expert now” tab, then again navigated to “Home” tab. From there navigated to “About us” tab. Now when pressed back button, I navigates back to “Home” tab. (Its normal behavior), but when again pressed back button, the app should exit instead it goes to “Call Expert Now” tab. I want that the app should exit when back button is pressed in Home screen and not navigate back within the app. Reference source code https://github.com/kalyan4github/smartproservices.git

kalyan4github avatar Nov 13 '14 11:11 kalyan4github

Hi, I have a simple login based application in which user can access About menu with or without login, issue came with android hardware back button,

Login screen > About page > From Menu go to Login > Press hardware back button > Application Should get closed rather go to About page.

Below is the code for navigation to login screen.

$scope.login = function() {
            $ionicHistory.nextViewOptions({
                disableBack: true,
                disableAnimate: true,
                historyRoot: true
            });
            $ionicHistory.clearCache();
            $ionicHistory.clearHistory();
            $ionicSideMenuDelegate.toggleLeft();
            $state.go('login');
        };

I am using ionic rc release 4 currently.

msalman avatar Apr 30 '15 09:04 msalman

+1

jayrmotta avatar Sep 29 '15 15:09 jayrmotta

Hi, I am facing an issue with hardware back button in Windows. Below code works fine in Andriod- $ionicPlatform.onHardwareBackButton(function () { console.log('back button event'); var token = localStorage.getItem('token'); if (token == null || token == undefined) { navigator.app.exitApp(); } }) But same code doesnt work in windows phone. When we click on hardware back button this function does not get invoked. Instead it exits the application and shows the menu screen.

Please let me know if there is a way to customize windows hardware back button to function like history.go(-1) so that it navigates to mobile app screen rather than exiting.

sarisam99 avatar Oct 27 '15 09:10 sarisam99

in App.js $ionicPlatform.registerBackButtonAction(function(event) { if($state.current.name=="tab.dash") { // your check here $ionicPopup.confirm({ title: 'System warning', template: 'are you sure you want to exit?' }).then(function(res) { if (res) { ionic.Platform.exitApp(); } }) } }, 100);

amitandroid22 avatar Oct 30 '15 10:10 amitandroid22

hi friends iam working in ionic 3 tabs project in this project tabs page three tabs is there. if any tab i click any button its go to next page and if i click back button its go to tabs page but my problem is if i click hardware back button app will be closed not go to tab page. can you please help me . i search so many sites but its showing blank project examples please help me friends

orengbasha avatar Feb 14 '18 07:02 orengbasha

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 01 '18 09:09 ionitron-bot[bot]