How do I report that an activity has been covered by the notification area?

Normally, Android calls onPause on your Activity when it starts to hide or hide, and then onStop when it no longer displays. In my game, I pause the game in onPause , so the user does not lose the game by looking elsewhere.

However, when the user drags the notification bar, it closes my Activity , but neither onPause nor onStop . This does not seem to be mentioned in the documentation. The game disappears in the background and no one is looking at it. Is there any other way to say that my Activity was hidden when this happens, so I can pause the game before the user loses? I can not find anything about this on the Android developer site.

+6
source share
2 answers

Since StatusBarManager is not part of the official API, I find it unlikely that there is a way to detect it. Even when using reflection, none of the status bar classes has a hook for listeners.

If possible, you can disable the status bar . Otherwise, I think you're out of luck :(

+3
source

onWindowFocusChanged(boolean hasFocus) Activity should fulfill the desired goal. It is called with false when the notification area is dragged down, and true after the area is dragged back. The relevant Android documentation says that this method "is the best indicator of whether this action is visible to the user." It also explicitly states that the callback is triggered when the status bar notification bar is displayed.

It is important to note that this method is also called in other situations. A good example is displaying an AlertDialog . onWindowFocusChanged even called when the activity itself shows an AlertDialog . This may require consideration, depending on whether your game uses AlertDialogs or something else that causes a change in focus.

In a scenario similar to that described in this question, we have successfully used the onWindowFocesChanged method, for example. on a Nexus 5 with Android 4.4 or a Sony Xperia tablet with Android 4.1.

+8
source

Source: https://habr.com/ru/post/926746/


All Articles