I have problems with fragment when the screen orientation changes.
In my code, I have create different layout xml according to the screen orientation.
For example, I have header_landscape.xml and header_portrait.xml in the layout directory. And each different header has the same fragment in the linear layout. So when I turn my device, I have the error “duplicate id …” that corresponding to my fragment.
The differences between layout is the content.When I am in “landscape” I display more informations than I am in “portrait”.
On create of my activity :
setContentView(R.layout.main_landscape);
//header
date=(TextView)findViewById(R.id.headerLandscapeDate);
routeSens=(TextView) findViewById(R.id.headerLandscapeRouteSens);
pkHeader=(TextView) findViewById(R.id.headerLandscapePk);
//Récupération de la listview créée dans le fichier main.xml
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
$ The code of the modification of layout when the orientation screen changes.
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE && isLoaded && oldScreenStateOrientation!=newConfig.orientation)
{
setContentView(R.layout.main_landscape);
//header
date=(TextView)findViewById(R.id.headerLandscapeDate);
routeSens=(TextView) findViewById(R.id.headerLandscapeRouteSens);
pkHeader=(TextView) findViewById(R.id.headerLandscapePk);
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT && isLoaded && oldScreenStateOrientation!=newConfig.orientation)
{
setContentView(R.layout.main_portrait);
routeSens=(TextView) findViewById(R.id.headerPortraitRouteSens);
pkHeader=(TextView) findViewById(R.id.headerPortraitPk);
maListViewPerso = (ListView) findViewById(R.id.ListeChoix);
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(chargeMenu());
}
}
If anyone have a solution.
I developp with the api8 (compatibility library fragment).
Sorry for my english.
Thanks
You should let android do all that configuration switching for you! Don’t do anything in “onConfigurationChanged”, remove all configuration changes from your manifest even!
Put your portrait layout in
layout/ folder
and put your landscape layout in
layout-land/ folder
Make sure they are both named: main.xml (or anything, as long as it is the same)
Then in your activity.onCreate, do something like this:
This way, you can let android worry about which configuration to use. If you only want to display the landscape view on large screens, you could put it in a folder such as layout-w1024dp. This way you can have multiple layouts very easily.