I need to replace my layout on orientation change and keep my views state and listeners (ex. in portrait mode I change a TextView color from blue to red and when I rotate the device the new layout with the same TextView needs to be red). I have 2 layouts: one in layout/ and one in layout-land/ and I added in AndroidManifest.xml this: android:configChanges="orientation|keyboardHidden|screenSize"
and this is my onConfigurationChanged
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDisplayWidth = Helper.getDisplayWidth(context);
mDisplayHeight = Helper.getDisplayHeight(context);
mDisplayOrientation = getResources().getConfiguration().orientation;
if(mDisplayOrientation == Configuration.ORIENTATION_LANDSCAPE){
//landscape
}else{
//portret
}
final View newView = View.inflate(getActivity(), R.layout.harta, null);
ViewGroup rootView = (ViewGroup) getView();
rootView.removeAllViews();
rootView.addView(newView);
}
The problem is that the new layout doesn't keep the old state and listeners, how can I achieve this?