Why isn't onConfigurationChanged being called?

I am writing some code that changes the text of the TextView when the orientation changes I am using the onConfigurationChanged () listener to change the text

override fun onConfigurationChanged(newConfig: Configuration?) {
    super.onConfigurationChanged(newConfig)

    val orientation : Int = getResources().getConfiguration().orientation
    val oTag = "Orientation Change"

    if(orientation == (Configuration.ORIENTATION_LANDSCAPE)){
        mytext?.setText("Changed to Landscape")
        Log.i(oTag, "Orientation Changed to Landscape")

    }else if (orientation == (Configuration.ORIENTATION_PORTRAIT)){
        mytext?.setText("Changed to Portratit")
        Log.i(oTag, "Orientation Changed to Portratit")
    }else{
        Log.i(oTag, "Nothing is coming!!")
    }
}

      

but nothing happens even in the log

Also I added this attribute to my activity in the manifest file

android:configChanges="orientation"

      

What am I missing here? also is there any other way to achieve this?

+3


source to share


1 answer


Use this code to get config

override fun onConfigurationChanged(newConfig: Configuration?) {
        super.onConfigurationChanged(newConfig)
        if (newConfig != null) {
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            }
        }
    }

      

A few things to try:



android:configChanges="orientation|keyboardHidden|screenSize"

, but not android:configChanges="orientation"

Make sure you don't call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

anywhere. This will cause onConfigurationChange () to fail.

+3


source







All Articles