Clicking the device back button closes the app instead of going back to previous page

后端 未结 5 1392
北海茫月
北海茫月 2020-11-30 06:04

If click any ion-item it open the desired page but if i click device back button it close the app rather than going back to previous page in android:

Th

5条回答
  •  广开言路
    2020-11-30 06:56

    The default behaviour of the back button is as follows: Go back in history - if the history stack is empty -> exit the app.

    So you should check the $state you are in, when you tap the hardware back button.

    With the following code (placed in the run function of your module) you can overwrite the default behaviour. For example you can disable the app exit like this:

    $ionicPlatform.registerBackButtonAction(function (event) {
      if($state.current.name=="app.home"){
        navigator.app.exitApp(); //<-- remove this line to disable the exit
      }
      else {
        navigator.app.backHistory();
      }
    }, 100);
    

    See the documentation for $ionicPlatform.

提交回复
热议问题