My problem with that is that when I switch to my horizontal view, and then back to my vertical view, I now have a duplicate layout for at least one of the nested fragments.
My guess is that this is because you are always adding the fragment in onCreate()
. Android automatically recreates fragments on configuration changes. Hence, onCreate()
should check to see if the fragment already exists before adding it:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content)==null) {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content,
new RotationFragment()).commit();
}
}