我有一个使用actionbarsherlock的应用 我用这个
android:configChanges="orientation"
清单文件中的停止在旋转时重新创建选项卡片段
现在轮换,片段不会重新创建并且工作正常,但是标签卡住了,我无法选择另一个标签,直到我回到原来的方向。
这是我的主要活动
public class class_main extends SherlockFragmentActivity implements ActionBar.TabListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_main);
getSupportActionBar()
.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab newTab0 = getSupportActionBar().newTab();
newTab0.setText("main");
newTab0.setTabListener(this);
ActionBar.Tab newTab1 = getSupportActionBar().newTab();
newTab1.setText("News");
newTab1.setTabListener(this);
ActionBar.Tab newTab2 = getSupportActionBar().newTab();
newTab2.setText("Calendar");
newTab2.setTabListener(this);
getSupportActionBar().addTab(newTab0);
getSupportActionBar().addTab(newTab1);
getSupportActionBar().addTab(newTab2);
if (savedInstanceState != null) {
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_class_main, menu);
return super.onCreateOptionsMenu(menu);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
{
if(tab.getPosition()==0)
{
mainFragment frag = new mainFragment();
ft.replace(android.R.id.content, frag);
}
else if(tab.getPosition()==1)
{
newsFragment frag = new newsFragment();
ft.replace(android.R.id.content, frag);
}
else if(tab.getPosition()==2)
{
calendarFragment frag = new calendarFragment();
ft.replace(android.R.id.content, frag);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
}
}