TabHost пусто при вращении Android с помощью onConfigurationChanged
У меня проблема с onConfigurationChanged и TabHost. Поскольку моя активность перезапускается при ротации, я нашел этот очень полезный пост: перезапуск активности при ротации Android
Я сортирую элементы интерфейса в функцию InitialGui()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InitialGui();
}
Переопределите onConfigurationChanged и вызовите InitialGui()
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
InitialGui();
}
Внутри AndroidManifest
<activity
android:label="@string/app_name"
android:name=".MyAndroidAppActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation"
>
Для простоты я создаю новый проект на основе http://www.mkyong.com/android/android-tablayout-example/ только с упомянутыми изменениями. Это кстати функция InitialGui
public void InitialGui()
{
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
// Android tab
Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Android")
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Apple")
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setIndicator("Windows")
.setContent(intentWindows);
// Blackberry tab
Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setIndicator("Berry")
.setContent(intentBerry);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
Моя проблема - пустые TabHosts после поворота, не имеет значения, по какой вкладке нажата ничего не отображается. Любой намек? Спасибо
1 ответ
Я думал, что сложный, нет необходимости setContentView(R.layout.myLayout); или что-то другое. Просто предотвратите вызов метода onCreate, переопределив onConfigurationChanged.
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
это работает для меня.