How to clear the navigation history in ionic framework

3

Hello community, have a question about the use of $ ionicHistory , which is part of Navigation the javascript / angular extensions.

I would like to know how to avoid going back when it is in one of the two menus, depending on the role of the user who has been admitted.

I would like to resolve the following concerns

  • You can only give the back button if the view is not that of the menu (either student or teacher)
  • In the case of Android, there is a back button, whether it is physical or digital, if it is in the view or template of any of the two menus, then, when giving back, you must exit the application
  • Avoid saving routes or views in this history, to avoid conflicts in the future.
  • asked by Pedro Miguel Pimienta Morales 11.03.2016 в 18:11
    source

    1 answer

    2

    Your problem can be solved by using the clearHistory() of service $ionicHistory method. Both in your student or teacher view controller you can call that method and this will prevent you from being able to navigate backwards since that method deletes the history except the current view (that is why it is necessary that you do it in the view of the student or professor).

    ...
    .controller('EstudianteCtrl', function($ionicHistory) {
        ...
        $ionicHistory.clearHistory();
    });
    

    You can also use the nextViewOptions() method and disable the back button in this way

     $ionicHistory.nextViewOptions({
         disableBack: true // Esto pone en 'null' la vista anterior
     });
    

    But this must be done from the previous view (The view "loading") since that applies to the next view to be displayed before transitioning to it.

    Finally to make your application exit by pressing the back button you have to use the service $ionicPlatform using onHardwareBackButton but you are entering inadvertently into a problem:

  • If you leave your application when doing back, you run the risk that your users leave the application unwittingly, especially if you have many in-depth views in your navigation. Back ..., Back ..., Back ..., Exit ... Ups !!!
  • Most mobile applications implement a way to exit when they reach the home view using the back button, which has become practically a standard.
  • You want to keep users inside your application and not outside it;)
  • The solution that I see to that is to use two backs instead of one showing a toast with a message with something like "Press again to exit"

    The toast can be installed with a plugin that you can find in link using

    cordova plugin add cordova-plugin-x-toast
    

    Then in the event platformReady you set the event (to make sure that the plugin has loaded)

    $ionicPlatform.ready(function () {
        var closeApp = false;
        $ionicPlatform.onHardwareBackButton(function () {
                if ($state.is('mivistaraiz')) {
                    if (closeApp === true) {
                        navigator.app.exitApp();
                    }
                    else {
                        closeApp = true;
                        $timeout(function () {
                            closeApp = false;
                        }, 2000);
                        notifications.notify("Presione nuevamente para salir", "short", "bottom");
                    }
                }
            });
    });
    

    You can change the timeout to make it bigger but basically what you do is exit if you press the back in a 2-second interval, otherwise it will show the toast again, thus preventing your users from leaving the application by accident.

        
    answered by 11.03.2016 / 18:18
    source