Esta lição descreve como registrar um listener para que seu app possa ser notificado sobre mudanças na visibilidade da IU do sistema. Isso é útil se você quer sincronizar outras partes da sua interface com a ocultação/exibição de barras do sistema.
Registrar um listener
Para receber notificações sobre mudanças na visibilidade da IU do sistema, registre um
View.OnSystemUiVisibilityChangeListener
na visualização.
Geralmente, essa é a visualização que você está usando para controlar a visibilidade da navegação.
Por exemplo, você pode adicionar este código ao método
onCreate()
da atividade:
Kotlin
window.decorView.setOnSystemUiVisibilityChangeListener { visibility -> // Note that system bars will only be "visible" if none of the // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) { // TODO: The system bars are visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The system bars are NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } }
Java
View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { // Note that system bars will only be "visible" if none of the // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { // TODO: The system bars are visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The system bars are NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } } });
Em geral, é recomendável manter a IU sincronizada com as mudanças na visibilidade da barra do sistema. Por exemplo, é possível usar esse listener para ocultar e mostrar a barra de ações em um show com a barra de status oculta e exibida.