capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

appStateChange fired too late

Open no-creative-name opened this issue 3 years ago • 4 comments

Bug Report

Plugin(s)

@capacitor/app

Capacitor Version

@capacitor/cli: 3.0.1 @capacitor/android: 3.0.1 @capacitor/core: 3.0.1 @capacitor/ios: 3.0.1

Platform(s)

Android

Current Behavior

Since for our app security/data privacy is a huge factor, we need to implement a way that the app content is hidden (white screen) when the app is backgrounded (e.g. in the task manager). For this is wanted to use the "appStateChange" event listener from @capacitor/app. The problem is that the event only fires when coming back into the app after putting it into the background. This makes it unusable for our use case.

Expected Behavior

"appStateChange" also fires before/when the app is backgrounded.

Other Technical Details

Tested on a real Android device and a simulated one.

no-creative-name avatar Jun 16 '21 13:06 no-creative-name

I found https://github.com/capacitor-community/privacy-screen to be able to hide the screen in the task manager. Still I think this should work as expected.

no-creative-name avatar Jun 18 '21 06:06 no-creative-name

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron 💙

Ionitron avatar Jul 06 '22 17:07 Ionitron

This issue does not need any more information.

In order to reproduce the issue just build an Android app using the plugin and use the supplied example:

import { App } from '@capacitor/app';

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

While debugging the app enable the debug console in Android Studio and look for App state changed. Is active? and you will see that only the resume events are handled.

In order to reproduce the pause/resume behavior just leave and re-enter the app on your phone.

The behavior in Android is buggy while in iOS it works as expected.

shpasser avatar Jul 11 '22 07:07 shpasser

I think that the actual issue is here:

https://github.com/ionic-team/capacitor/blob/ee2069a6e9d3d52e232a31d35909faf87c272c7c/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java#L91

The function should call bridge.getApp().fireStatusChange(false) but it doesn't.

shpasser avatar Jul 11 '22 07:07 shpasser

Sorry, but I can't reproduce. the event is being fired on onStop, not on onPause, that's why the code is not on that line, but it's being fired on my app.

There is an feature request open for including more states so it could be fired on pause. https://github.com/ionic-team/capacitor-plugins/issues/1153

Also, for Cordova compatibility we also fire the cordova pause event, you can use it like this: document.addEventListener("pause", yourCallbackFunction, false);

jcesarmobile avatar Sep 02 '22 16:09 jcesarmobile