Srinidhi
Srinidhi

Reputation: 90

App close event in Ionic Windows 8.1 App?

I'm building a Windows 8.1 App using Ionic 3. In the app, the user would have to type in a lot of stuff. I want to avoid accidental closure of the app by clicking on 'X' button at the top right corner or by Alt+F4. When the user tries to close the app, is there a way to prevent the default behaviour of closing the app and instead ask a confirmation?

Upvotes: 1

Views: 105

Answers (2)

Srinidhi
Srinidhi

Reputation: 90

There's no special event to indicate that the user has closed an app.

App Lifecycle docs for Windows Store Apps. https://learn.microsoft.com/en-us/previous-versions/windows/apps/hh464925(v=win.10)?redirectedfrom=MSDN#app-close

When the user closes the app or moves the app to background, it enters into a Suspended State. Windows recommends saving user state when the app enters such a state. You can save data in that state thereby achieving the overall goal.

Upvotes: 0

andreszs
andreszs

Reputation: 2956

You must override the onbackbutton event to do this.

document.addEventListener('backbutton', function (evt) {
    /* BackButton pressed: do nothing */
    return;
}, false);

Do NOT use the method shown on that page to exit the app:

throw new Error('Exit'); // This will suspend the app BUT the store will reject it

If you do this to exit your app, it will be rejected by the Microsoft store. To exit the app, remove the event listener and let the backbutton event suspend the app normally.

Upvotes: 1

Related Questions