Search code examples
javascriptcapacitor

How to check if the application is foreground or background now?


Capacitor provides an App plugin that allows to get a call back when the application goes background or foreground. But how to get the application isActive value (a.k.a. foreground/background state) at any moment? The problem is that I don't know the application state until I subscribe to the appStateChange event AND the app goes background or foreground.


Solution

  • A method to get the current state was added recently:

    import {Plugins} from '@capacitor/core';
    
    const {App} = Plugins;
    
    let {isActive} = await App.getState();