I am aware this has been asked a huge number of times, but I just cannot so get this to work. I am using two constraint layouts, and the switching between them works fine. However, the values on screen are reset, as per a million other questions here. This behaviour seems wildly inconsistent, as I've used it before in another project (where it worked for one activity but not another one), but I cannot get it to work now.
I am running my code on the android emulator, android 7.1.1, API 25
I have put
android:configChanges="keyboardHidden|orientation|screenSize"
in
<activity>
in androidManifest.xml
and
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_my_activity_land);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_my_activity);
}
}
in my relevant activity.
The orientation changes and the layouts are loaded correctly, but the values are still reset.
I have looked at these answers and lots of similar ones:
Android activity restarted at orientation change even with configChanges set
Save data and change orientation
Any way I can do this without doing like this suggests? (recreating the activity):
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
Any help is greatly appreciated!