onConfigurationChanged is not getting called any time in android

左心房为你撑大大i 提交于 2019-12-01 07:26:04

问题


I have create sample program to test Orientation. If portrait mode and I tilt the phone the other way round I want my sample app to reversePortrait. After reading lot of questions on this - Figured we need to need use:

   onConfigurationChanged(Configuration newConfig) 

I was trying out the following:

    @Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);

    Configuration c = getResources().getConfiguration();

    Log.e("Config",""+c);

    if (c.orientation == Configuration.ORIENTATION_PORTRAIT) 
    {
        // portrait

        Log.e("On Config Change","portrait");

    } 
    else if (c.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        // landscape
        Log.e("On Config Change","LANDSCAPE");

    }
}

manifest.xml

android:configChanges="orientation" 

tried even this

android:configChanges="orientation|screensize"

Strange I am not getting logs inside OnConfigurationChange method.

Not sure. What is the problem here?

I am using minimum api 8 to target Api 18.

Can somebody help me out with this?

Thanks!


回答1:


I had a same issue after changing target API to 19(4.4 kitkat) You need to add layoutDirection attribute for targeting higher than version 17.

my attribute looks like:

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|layoutDirection"

The key is to add 'layoutDirection'




回答2:


I just encountered this problem, and fixed it with this weird little trick.

Set your screenOrientation in your manifest, like so:

android:screenOrientation="sensor"

That fixed it for me.




回答3:


Your code is no problem Modify your manifest.xml android:configChanges="orientation|keyboardHidden" I hope it can help you



来源:https://stackoverflow.com/questions/17960525/onconfigurationchanged-is-not-getting-called-any-time-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!