Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for detecting app focus events. #69

Merged
merged 9 commits into from
Jan 26, 2024

Conversation

proneon267
Copy link
Contributor

@proneon267 proneon267 commented Aug 28, 2023

Added support for detecting app focus events through: onWindowFocusChanged https://developer.android.com/reference/android/view/ViewTreeObserver.OnWindowFocusChangeListener#onWindowFocusChanged(boolean)

This PR relates to the PR: beeware/toga#2096

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

@mhsmith
Copy link
Member

mhsmith commented Aug 29, 2023

As it says at https://developer.android.com/reference/android/app/Activity#onWindowFocusChanged(boolean):

a foreground activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows have it.

So I think onPause and onResume would be more appropriate here.

@mhsmith
Copy link
Member

mhsmith commented Aug 29, 2023

Also, now that I remind myself of how this works, there's no need to add any methods to IPythonApp, because MainActivity doesn't use it anymore. Instead, MainActivity.userCode should be altered to check whether each method exists on the Python object before trying to call it.

@proneon267
Copy link
Contributor Author

proneon267 commented Aug 30, 2023

So I think onPause and onResume would be more appropriate here.

These events are only triggered when the app state changes between [visible to user] and [not visible to user].

They do not take the input focus into account when the app is [visible to user but not receiving inputs] or [visible to user and receiving inputs].

Hence, onPause and onResume will be best suited for handlers like: on_background[not visible to user] and on_foreground[visible to user].

onWindowFocusChanged is the only event that is consistently triggered when the focus is:

  • gained ([visible to user and receiving inputs]) or
  • lost ([visible to user but not receiving inputs],[not visible to user])

Moreover, as per: https://source.android.com/docs/core/display/multi_display/multi-resume , multiple apps can be in Resumed state, and onPause event is not triggered when the input focus changes between apps in multi-app mode.
So, properly detecting focus is not possible with onPause and onResume.

Although, I do agree with you that onPause and onResume will be needed to be used as popups and other dialogs will take away input focus. I am trying out a few things and will let you know the results when I will get a consistent behavior.

@proneon267
Copy link
Contributor Author

According to: https://developer.android.com/reference/android/app/Activity#onWindowFocusChanged(boolean)

Starting with Build.VERSION_CODES.Q there can be multiple resumed activities at the same time in multi-window mode, so resumed state does not guarantee window focus even if there are no overlays above.

Hence, I have chosen: onTopResumedActivityChanged https://developer.android.com/reference/android/app/Activity#onTopResumedActivityChanged(boolean)
This works better than onWindowFocusChanged and ignores focus gained by dialogs and popups from the same app.

On platform versions prior to Build.VERSION_CODES.Q, I have implemented the feature with onPause and onResume.

@mhsmith mhsmith merged commit 3a3786d into beeware:main Jan 26, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants