Reputation: 735
I'm developing the typical tablet app with fragments: a list of elements on the left side and the content on the right side. For the smartphone version I show only one fragment with the list of elements and for the tablets I show 2 fragments with the list and contents.
In order to handle the screen rotation, I'm handling the rotations on my app. When the user rotates the screen and I'm showing an image or some text on the right side, the I show it fullscreen:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged");
if(MainFragmentActivity.isTablet)
{
FragmentManager fm = getSupportFragmentManager();
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE && !frameTabs.isShown()) //Show again the tabs fragment
{
Utils.goNormalScreen(this);
frameTabs.setVisibility(View.VISIBLE);
}
else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT && (fm.findFragmentByTag(Utils.SHOW_IMAGE_TAG) != null || fm.findFragmentByTag(Utils.SHOW_TEXT_TAG) != null
|| fm.findFragmentByTag(Utils.SHOW_WEB_TAG) != null)) //Hide tabs fragment
{
Log.i(TAG, "Detected showing image");
Utils.goFullScreen(this);
frameTabs.setVisibility(View.GONE);
}
}
}
The problem is the 7 inch tablet:on portrait mode I want to show the smartphone version (1 fragment with a list) and on landscape the tablet version with 2 fragments.
So in the onConfigurationChanged() I need to detect the 7 inch tablet and recreate the activity in order to change the layout, isn't it? how to do that? is there any other better way to make it work?
Should I recreate the activity getting all the data again from the server (ListView) or is it better to save the data on a bundle and reuse it when rotated?
Upvotes: 1
Views: 1536
Reputation: 134664
First, stop handling configuration changes, then put the 2 fragment layout in layout-sw600dp-land
and leave the others as they are. Now Android will determine to use that layout automatically upon rotation.
Upvotes: 3