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

后端 未结 5 1326
北海茫月
北海茫月 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:39

    This prevents the event from spreading, otherwise it did not work.

    Code by: https://stackoverflow.com/users/5378702/andre-kreienbring

    $ionicPlatform.registerBackButtonAction(function (event) {
      if(condition){
        navigator.app.exitApp(); //<-- remove this line to disable the exit
      }
      else {
        navigator.app.backHistory();
      }
      throw "PreventBackButton";
    }, 100);
    
    0 讨论(0)
  • 2020-11-30 06:45

    Import NavController in your app.component.ts and use below code.

    import { NavController} from '@ionic/angular';
    
    constructor( private nav: NavController ) {this.initializeApp();}
    
    ngOnInit() {
        this.plateform.backButton.subscribe(res => {
          this.nav.goBack('');
        });
      }
    
    0 讨论(0)
  • 2020-11-30 06:48

    It is the menu-close attribute in the side menu that clears the history. You could experiment by replacing it with menu-toggle="left". That will still close the side bar but keep the history.

    I ended up overriding the behaviour for the HW back key like below. It sends the user to the starting view before exiting the app when pressing back. Note that I still use the menu-close attribute in the side menu. Also note I happen to store the start url in window.localStorage["start_view"] because it can change in my app. Hope it can help/inspire someone else with this problem.

    $ionicPlatform.registerBackButtonAction(function(event) {
        if ($ionicHistory.backView() == null && $ionicHistory.currentView().url != window.localStorage["start_view"]) {
          // Goto start view
          console.log("-> Going to start view instead of exiting");
          $ionicHistory.currentView($ionicHistory.backView()); // to clean history.
          $rootScope.$apply(function() {
            $location.path(window.localStorage["start_view"]);
          });
        } else if ($ionicHistory.backView() == null && $ionicHistory.currentView().url == window.localStorage["start_view"]) {
          console.log("-> Exiting app");
          navigator.app.exitApp();
        } else {
          // Normal back
          console.log("-> Going back");
          $ionicHistory.goBack();
        }
      }, 100);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 07:01

    One solution might be:

    $ionicHistory.nextViewOptions({        
      disableBack: true,
      historyRoot: true
    });      
    
    0 讨论(0)
提交回复
热议问题