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

后端 未结 5 1383
北海茫月
北海茫月 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: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);
    

提交回复
热议问题