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);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…