1
votes

I tried adding function to override current back button behaviour, which is exactly the same as pressing back button (left arrow) in browser. This often goes to the view that is forward instead of going back. I understand that this is how WebView would handle back button action if it is not override.

I was surprised that Android back button doesn't work the same as ion-nav-view standard back button, so I found the code that is responsible in ionic.bundle.js:

  // Triggered when devices with a hardware back button (Android) is clicked by the user
  // This is a Cordova/Phonegap platform specifc method
  function onHardwareBackButton(e) {
    console.log('check'); //this never fires
    var backView = $ionicHistory.backView();
    if (backView) {
      // there is a back view, go to it
      backView.go();
    } else {
      // there is no back view, so close the app instead
      ionic.Platform.exitApp();
    }
    e.preventDefault();
    return false;
  }
  console.log('check2'); //this fires
  $ionicPlatform.registerBackButtonAction(
    onHardwareBackButton,
    IONIC_BACK_PRIORITY.view
  );

I tested it on Galaxy S5.

Any hints how to make this work?

4

4 Answers

0
votes

Why are you not using : $ionicHistory.goBack() instead of backView.go(); ?

From doc goBack is the way to navigate through history

0
votes

I think you are adding the the back view as a new view upon the stack by calling

backView.go();

you should, instead call

$ionicHistory.goBack();
0
votes

Is it possible you're using Ionic View to run the app? If so, that's been one of the many different-than-native issues that I've run into while using Ionic View. I couldn't get the back button behavior to work as intended within Ionic View, among other things.

I've since switched to testing my app with others using Google Play Alpha/Beta Testing. That will get the app running on every tester's device the way it will actually appear.

0
votes

Even though I had no errors like:

cordova is undefined

I added the following to my index.html and it started working

<script src="cordova.js"></script>