ionic-frameworkionic-vue

Run action before Ionic Vue closes or app runs in background


How can i detect in Ionic Vue if the app closes or if the app is send to the background?

this.platformor platform isn't available or does not work?!


Solution

  • You can subscribe to the appStateChange event, like described here: https://capacitorjs.com/docs/apis/app

    Example:

    import { App } from '@capacitor/app'
    
    export default {
      created () {
        App.addListener('appStateChange', state => {
          if (!state.isActive) { // if isActive is true, App got sent to foreground, if it is false, it got sent to the background
            ...
          }
        })
      }
    }