This is what I ended up coming up with. It was tested and works for Ionic with React. It’s a capacitor plugin:
This will trigger when the app is active. I think if you use the else statement it will trigger when the app is minimized, but I don’t recall testing that part. Someone can test and report back.
//import the plugin at the top
import { Plugins, AppState} from '@capacitor/core';
const { App } = Plugins;
App.addListener('appStateChange', (state: AppState) => {
if (state.isActive == true) {
// Do this when app is active
} else {
// Do this when app is inactive
}
});
Additional Sources:
https://capacitorjs.com/docs/apis (all the capacitor plugins)
https://stackoverflow.com/questions/62234807/ionic-event-not-triggered-when-app-goes-in-background-plattform-pause
import { App } from '@capacitor/app';
App.addListener('appStateChange', ({ isActive }) => {
// console.log('App state changed. Is active?', isActive);
if (isActive) {
// Do fun stuff.
}
});