简体   繁体   中英

Android: How to prevent activity from reloading after orientation change?

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"

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM