How to hold back twice to exit

hey guys,

i don’t know if this is still relevant but I discovered something I couldn’t find in over a year of googling. In order to have the cleanest way possible to handle back button quit without having to mess with back-button defaults, use this snippet:

good luck :slight_smile:

        this.oldexitappfn = this.platform.exitApp;
        this.platform.exitApp = () => {
            if (/* condition to exit app is present */) {
                this.platform.exitApp = this.oldexitappfn;
                this.platform.exitApp();
            } else {
                /* don't exit and do something else */
            }
        }

I found this out without any reference online so this might be the first one, and it works :slight_smile:

I guess we have different notions of “clean”, because what you have posted goes to the heart of one of the things I hate the most about JavaScript: the ability to reach inside the internals of other entities and change their behavior. IMHO, one should never ever do anything like this.

Back in the days of old 68K MacOS, we called this technique “trap head patching”, and it was the cause of INIT conflict hell that greatly undermined the stability of the OS. Platform belongs to the framework; it does not belong to application code, and application code should not modify its internals.

When a publicly documented method like registerBackButtonAction exists, use it, instead of poking around in framework guts.

i can tell you what the problem is, The problem is Ionic.

They offer all the nice things like cool standards for mobile UI and everything else and it drags you for months and months into dev when you realize you hit a wall. In my case that wall was Tabs.

In order to push pages OUT of the tabs i needed to use nav.parent.parent.push all over the app, which resulted in the creation of multiple rootNavs which are not retrievable in the Ionic code so I can’t check if any pages are pushed before i unload the app using the global override of the back button method.

Why i say clean? well, it’s because the provided method probably doesn’t suit the majority of the developers. Why would I want to override the button throughout the entire app when usually I just want to override it in one-specific-very-specific-case. Unfortunately Ionic doesn’t provide that. They could’ve but they didn’t.

I understand where your concern comes from, but all I can say is: use at your own risk (which is very low risk)

Thanks

I hit that wall as well, and after lots of reflection decided that what I was trying to do wasn’t a good fit for the UI contract that tabs present. When I restricted my use of tabs to situations where it naturally fit and I did not get frustrated, I became much happier. In any situation where something that looks sort of like tabs was desired, I have found segments very useful.

This confuses me, because Platform is an app-wide singleton. When you assign directly to its prototype in one place, I would expect that to affect everything app-wide, exactly as patching a Toolbox A-trap affected everything running on the machine.

If you want to modify back button behavior only in one specific place (such as when a particular page is active), what I would do is to use lifecycle events on that page to register and deregister the custom back button action, thereby insulating the rest of the app from any unexpected effects. I have used that approach to get around a bug involving back buttons and modal dialogs.

well obviously if i want to manipulate the back button on one specific page the “cleaner” method is to use the Ionic given lifecycle methods such as IonViewWillUnload for example. In my case i only manipulated the exitApp() method which AFAIK only is called when the app is about to leave through the back button :smiley:

Anyway, to keep this short, you are right, I don’t prefer altering the guts of the framework but you gotta do what you gotta do, and in this case it’s not a super crucial method being used throughout the app so yeah, if you like it use it, if you don’t then don’t. I wish someone would’ve shown me this snippet a year ago TBH.

Hi, I am using Ionic v4 and in Platform class has no method with exitApp(), how could exit app when clicked back button twice?
thnks

I retract my comments about how to override the back button on exit, it is very unreliable. sorry @rapropos, you were right :slight_smile: I did however find a way to override the back button and revert it to the old state when i don’t need it and I also published a package on npm that makes it easier to use these functions. I only started this project today and published it also today so I did minimal testing on it, but the functions that matter do work.
take a look:
https://www.npmjs.com/package/ionic3-android-backbutton

My code says “Property this.lastBack does not exist on type 'HomePage”, I need to import something else?