Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return to immersive mode after closing the keyboard on Android

I added the immersive mode into my app. Here is the code :

 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
     super.onWindowFocusChanged(hasFocus);
     if (hasFocus)
     {
         getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                 | View.SYSTEM_UI_FLAG_FULLSCREEN
                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
     }
 }

But if I type on the keyboard and I close it (with the back button, by clicking on the screen), the navigation bar stays displayed, I need to reduce/reopen the app to get back the immersive mode.

How can I return to immersive mode after closing the keyboard ?

EDIT : It's a Cordova app

like image 473
thomas-hiron Avatar asked May 04 '14 12:05

thomas-hiron


1 Answers

I used handlers to detect user inactivity and hide system ui thereafter. It automatically detect if user is not interacting on screen then auto hide system UI after 5 seconds

//Declare handler
private var timeoutHandler: Handler? = null
private var interactionTimeoutRunnable: Runnable? = null

In onCreate()

  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
. . .

       //Initialise handler
        timeoutHandler =  Handler();
        interactionTimeoutRunnable =  Runnable {
            // Handle Timeout stuffs here
            hideSystemUI()
        }

        //start countdown
        startHandler()

. . .

Method to handle focus change

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) hideSystemUI()
    }

Hide system Ui

    private fun hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }


    // Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
    private fun showSystemUI() {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    }



// reset handler on user interaction
override fun onUserInteraction() {
    super.onUserInteraction()
    resetHandler()
}

//restart countdown
fun resetHandler() {
    timeoutHandler!!.removeCallbacks(interactionTimeoutRunnable);
    timeoutHandler!!.postDelayed(interactionTimeoutRunnable, 5*1000); //for 10 second

}

// start countdown
fun startHandler() {
    timeoutHandler!!.postDelayed(interactionTimeoutRunnable, 5*1000); //for 10 second
}
like image 68
Hitesh Sahu Avatar answered Oct 01 '22 01:10

Hitesh Sahu