0

I look on the Internet how to prevent the activity from reloading on orientation change and I did what was suggested and it helped me solve my problem. But then a new problem appeared. In my activity I have some pictures and buttons that are invisible and would appear if a condition is met. But now when I rotate the phone the pictures and buttons appear although the condition is not met. Is there a way to prevent that from happening?

To stop the activity from reloading I added this code in my java file:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.activity_get_variants);
}

and in the Manifest file:

 android:configChanges="keyboardHidden|orientation|screenSize"
user3182266
  • 1,270
  • 4
  • 23
  • 49
  • It is already answered [HERE](http://stackoverflow.com/questions/5913130/dont-reload-application-when-orientation-changes). Hope that helps. – Viral Patel Mar 14 '14 at 07:52
  • You don't set the content view again it will change the layout again i mentioned below If you try that it will work fine You are setting the content view again that's the problem I think.... – Naveen Kumar Kuppan Mar 14 '14 at 07:57
  • add UIMode in your config changes android:configChanges="screenSize|orientation|keyboardHidden|UIMode". I hope ir will work for you – Yogendra Mar 14 '14 at 07:58
  • You can use fragments instead. Please check http://stackoverflow.com/questions/13305861/fool-proof-way-to-handle-fragment-on-orientation-change and http://stackoverflow.com/questions/13986630/saving-fragment-state-on-orientation-change – NullPointerException Mar 14 '14 at 08:01
  • This, `android:configChanges="keyboardHidden|orientation|screenSize"`, is a hack and you should not use it unless you **really** understand why. It's too much discussion for here, but there are many articles available which describe why. Even Google say do not use it. The problem is that you have not fixed the bugs in your code, you have hidden them, and there are many other ways that those bugs can occur. The correct answer is to learn about the Activity life cycle and code your callback methods correctly so that the bugs are fixed. You will not have success otherwise. – Simon Mar 14 '14 at 08:01

1 Answers1

0

Your code need some changes:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    switch(newConfig.orientation) {
        case Configuration.ORIENTATION_PORTRAIT:
            // do your code
            break;
        case Configuration.ORIENTATION_LANDSCAPE:
            // show images and button here
            break;
    }
}
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65