How to Detect When the Notification/System Bar Is Opened

How to detect when the notification/system bar is opened

Before I big with the implementation, I will give a brief explanation of my (very hacky) logic. When an Activity is no longer visible to the user for any reason, onWindowFocusChanged(..) gets invoked. However, onStop() only gets invoked when the Activity is no longer visible to the user by going to the background. I noticed that when switching Activities, onStop() is always invoked after onWindowFocusChanged(..), so I added a check in onWindowFocusChanged(..) to see if onStop() had already been invoked (with a 1 second delay), and I did this using the static member. Now for the how-to...

You will need a parent Activity that all the Activities in your app extend. In this parent Activity, add this static member:

private static boolean wasOnStopCalledAfterOnWindowFocusChanged;

Then in your onStop() method, add this line, make sure you invoke it BEFORE super.onStop()

@Override
protected void onStop() {
wasOnStopCalledAfterOnWindowFocusChanged = true;
super.onStop();
}

Finally, you need to override onWindowFocusChanged(..) in this parent Activity, and add in the below logic.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (!wasOnStopCalledAfterOnWindowFocusChanged) {

// NOTIFICATION BAR IS DOWN...DO STUFF

}
wasOnStopCalledAfterOnWindowFocusChanged = false;
}
}, 1000);
}
}

How to tell the activity has been covered by the notification area?

Since the StatusBarManager isn't part of the official API, I find it unlikely that there is a way to detect it. Even using reflection, none of the statusbar-classes seem to have a hook for listeners.

If it is feasible, you could deactivate the statusbar. Otherwise, I think you are out of luck :(

How to detect expanding of status bar?

There is no callback of any kind when the notification bar is dragged down on Android.

This is because Android apps are meant to be designed in a way that the notification bar coming up and going away does not affect the functioning in any way.

Detect that statusBar had collapsed

If it will help someone, I achieved my goal by adding some delay to the opening of the notification bar. this way the onWindowFocusChanged() method always called as needed.
So now my code implemented this way:

findViewById(R.id.status_bar).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent e) {
Utils.showBelTitleBar(ctx, false, updateClkBtry, handler);
ctx.handler.postDelayed(new Runnable() {
@Override
public void run() {
Utils.setNotification(ctx);
}
}, 100);
return false;
}
});


Related Topics



Leave a reply



Submit