Search code examples
androidandroid-layoutandroid-orientation

Application closes without any Error when orientation changes


i have created two layouts for portrait and landscape differently layout.xml is in both layout for portrait mode and in layout-land it works fine on some devices but when i run that application in some low configuration devices after two or three time of changing the orientation the application closes without giving any kind of error.

i have also included android:configChanges="orientation|keyboardHidden" in AndroidManifest.xml

P.S. there is one activity which has two xml for portrait and landscape and the activity contains a fragment which has two xml for portrait and landscape.

Edit onCreate of my code is here:

protected void onCreate(Bundle savedInstanceState) {


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    View v = findViewById(R.id.home_view);
    v.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);


    initViews();

    if (savedInstanceState != null) {

        //maintaining state
    }

}

Solution

  • First of all like everyone else pointed out, only using "orientation|keyboardHidden" is not enough

    You have to use screenSize as well..

    android:configChanges="orientation|keyboardHidden|screenSize"
    

    And in your activity.. You have to override the onConfigurationChanged(Configuration newConfig) method ..

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        setContentView(R.layout.activity_main);
        initViews();  // initialize your views again..
    
    }