Search code examples
androidclipboard

When to check the clipboard content in Android API 29 when app gets focus?


One of the privacy-related changes in API 29 is that an app does not have access to the clipboard unless it is "in focus".

I am trying to fix my app that has a Paste button in the main window (not in a menu), so I need to check if the clipboard has suitable data when the user launches or switches to the app (in order to decide if the Paste button should be enabled). I hoped that when the app is in OnResume, is it already allowed to access the clipboard, but it appears that in this state the getPrimaryClip still returns null even though the clipboard contains a primary clip.

Any suggestion as to which event I should wait for to be sure that my app is already allowed to access the clipboard?


Solution

  • Nevermind, I found the answer at How to use onWindowFocusChanged() method?.

    The trick is to check the clipboard within OnWindowFocusChanged when hasFocus boolean parameter is true.