我的标签在屏幕旋转时更改为第一个标签

时间:2013-03-15 11:06:29

标签: android rotation tabactivity screen-rotation

我正在创建一个tabActivity所在的Android应用程序。

我的标签活动的on create方法如下所示。

protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      
    setContentView(R.layout.logged);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_background);
    mytab=(TabHost)findViewById(android.R.id.tabhost);
    Log.e("id",mytab.getId()+"");
    mytab.setup();  
    setupTab( "AAAA","AAAA.class",tabImages[2]);
    setupTab( "BBBB","BBBB.class",tabImages[1]);
    setupTab("CCCC","CCCC.class",tabImages[0]);
    setupTab("DDDD","DDDD.class",tabImages[3]);
    setupTab("EEEE","EEEE.class",tabImages[4]);
}

通过搜索一些问题我得到了一些想法,即添加onSaveInstanceState(Bundle savedInstanceState)onRestoreInstanceState(Bundle savedInstanceState)。 下面给出了这两个函数的代码。

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("tab_id",current_tab);
    Log.e("tab",""+savedInstanceState.getInt("tab_id"));
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    mytab.setCurrentTab(savedInstanceState.getInt("tab_id"));
    Log.e("tab",""+savedInstanceState.getInt("tab_id"));
}

但它没有显示当前标签。它显示我的默认选项卡标签(0)..你可以帮助我。还有什么要在我的代码中添加。

1 个答案:

答案 0 :(得分:1)

尝试移动

mytab.setCurrentTab(savedInstanceState.getInt("tab_id"));

进入 onCreate()方法

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 
   // intialising and setup TabHost
   if (savedInstanceState != null) {
      mytab.setCurrentTab(savedInstanceState.getInt("tab_id"));
   }
}

现在它应该有效。