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
Swap Fragment in an Activity via Animation
How to Hide One Item in an Android Spinner
Android - Copy Existing Project with a New Name in Eclipse
Recompile with -Xlint in Android Studio
Okhttp Response Callbacks on the Main Thread
Post on Facebook Wall Using Facebook Android Sdk Without Opening Dialog Box
"No Enclosing Instance of Type" Error While Calling Method from Another Class in Android
How to Prevent a Scrollview from Scrolling to a Webview After Data Is Loaded
How to Set Recyclerview App:Layoutmanager="" from Xml
Android: Sample Code to Connect to Vpn in Android 4.0 Using Vpnservice API
How Can One Pull the (Private) Data of One's Own Android App
How to Set Custom Typeface to Items in Navigationview
Connectivity_Action Intent Received Twice When Wifi Connected
Support Library Vectordrawable Resources$Notfoundexception
How to Pass Custom Component Parameters in Java and Xml
Outdated Kotlin Runtime Warning in Android Studio
Android - Nestedscrollview Which Contains Expandablelistview Doesn't Scroll When Expanded