Platform Pause Resume not firing in Capacitor

Does anyone know when this will be resolved? Or an alternative?
I’m starting to regret using Capacitor for this app. I may have been a bit early in adopting.
I can 't use capacitor plugins either as I am on an old webview version (44). Although I can use some native plugins.

this.platform.pause.subscribe(() => {
//code…
});

Yeah, that’s what doesn’t work with Capacitor.

this.platform.pause.subscribe(() => {
 // Does not get called
}

this.platform.resume.subscribe(() => {
 // Does not get called
}

The support for this has been added in Capacitor Beta 22 and now works fine.

Where is the “this.platform” coming from?

I am having the same issues and I use:
import { Platform } from '@ionic/angular';

And I can not get this to run, or rather it never gets called:

      this.platform.pause.subscribe(() => {
        console.log('Pause subscribe was called');
      });

Using

    "@capacitor/android": "^1.2.0",
    "@capacitor/core": "^1.2.0",
    "@capacitor/ios": "^1.2.0",

Hi @Rolchau,

You need to inject it in your constructor.
Take a look in your app.component.ts file it will already be in there by default.

import { Platform } from '@ionic/angular';
constructor(private platform: Platform) {

platform.ready().then(() => {
      this.onPauseSubscription = this.platform.pause.subscribe(() => {
         // do stuff
        });

      this.onResumeSubscription = this.platform.resume.subscribe(() => {
        // do stuff
      });
    });
}

Thanks @wekas

That is however exactly what I am doing - it just never fires. The only thing I can get firing is the App.addListener(‘appStateChange’, …) and then a BackgroundTask beforeExit. But that only seems to execute when the app is completely in the background, not when switching to the multitask overview.

I guess I need a plugin of sorts to get it working the way I want it to.

That Capacitor version has only just been released so maybe it is not working again.
I only did it that way as I was using a very old version of android and an old webview.

Try doing it the capacitor way. I think it is as below (from memory)

import { Plugins, AppState } from '@capacitor/core';

const { App } = Plugins;

App.addListener('appStateChange', (state: AppState) => {
  // state.isActive contains the active state
  console.log('App state changed. Is active?', state.isActive);
});

https://capacitor.ionicframework.com/docs/apis/app

1 Like