In MY application the activity is entering in to the application not responding state when the configuration is changed from the portrait to landscape
and coming to the piece of the code for the configuration change is:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mWordSelCategory =mWordCategory;
Log.d(TAG,"THE CATEGORY IS :: "+mWordSelCategory);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.word_start);
mImagebackground = (ImageView) findViewById(R.id.mImgWord_ImageBg);
if (mWordSelCategory.equals("Animals")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_animals);
} else if (mWordSelCategory.equals("Fruits")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_fruits);
} else if (mWordSelCategory.equals("Things")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_objects);
} else if (mWordSelCategory.equals("Vegetables")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_vegetables);
}
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.word_start_horizt);
mImagebackground = (ImageView) findViewById(R.id.mImgWord_ImageBg);
if (mWordSelCategory.equals("Animals")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_hori_animals);
} else if (mWordSelCategory.equals("Fruits")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_horiz_fruits);
} else if (mWordSelCategory.equals("Things")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_hori_objects);
} else if (mWordSelCategory.equals("Vegetables")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_hori_vegetable);
}
}
getReconfigData();
}
Whoa, you got a lot of code duplication in there.
Refer to http://developer.android.com/guide/topics/resources/providing-resources.html and specifically check out the Providing Alternative Resources section – you can have the layout system use different layouts depending on whether your device is in landscape or portrait mode automatically.
E.g.
Then you will only have a single call to
setContentViewand all thefindViewByIds ….