在横向上,我有一个活动,其中previewFragment被加载到R.id.fragment_container_child(在屏幕的左边三分之一处),而selectionFragment加载到R.id.fragment_container_parent(右三分之二的屏幕)。选择第二个选项卡会将selectionFragment更改为contactFragment,但previewFragment保持不变。这一切都正常。
当方向改变时,布局会通过xml稍微改变,其中R.id.fragment_child现在占据底部三分之一,而不是左三分之一。当我改变方向时会出现问题。我的操作栏和片段消失了,虽然确认布局发生了变化。
我没有在清单文件中定义android:configChanges="orientation"
。以下是我的活动代码:
public class MainActivity extends Activity implements ActionBar.TabListener {
static SelectionFragment selectionFragment;
static ContactFragment contactFragment;
public static PreviewFragment previewFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container_child) != null) {
if (savedInstanceState != null) {
return;
}
previewFragment = new PreviewFragment();
selectionFragment = new SelectionFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.add(R.id.fragment_container_child, previewFragment,
"PREVIEW");
transaction.add(R.id.fragment_container_parent, selectionFragment,
"SELECTION");
transaction.commit();
}
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.menu)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.client_information).setTabListener(this));
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
}
@Override
public void onSaveInstanceState(Bundle outState) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
switch (tab.getPosition()) {
case 0:
if (selectionFragment == null) {
selectionFragment = new SelectionFragment();
transaction.add(R.id.fragment_container_parent,
selectionFragment, "SELECTION");
} else {
transaction.attach(selectionFragment);
}
break;
case 1:
if (contactFragment == null) {
contactFragment = new ContactFragment();
transaction.add(R.id.fragment_container_parent,
contactFragment, "CONTACT");
} else {
transaction.attach(contactFragment);
}
break;
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
switch (tab.getPosition()) {
case 0:
if (selectionFragment != null)
transaction.detach(selectionFragment);
break;
case 1:
if (contactFragment != null)
transaction.detach(contactFragment);
break;
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
答案 0 :(得分:1)
我无法理解你到底发生了什么,但这里有一些提示:
当您旋转屏幕时,Android会破坏并重建所有片段/活动。所以看看你的onCreate,onDestroy ...
如果您需要多屏应用,请查看here并为每个屏幕创建新的布局,以确保您的应用能够正常使用。