0

我正在创建一个 android 应用程序,其中有一个 tabActivity。

下面给出了我的选项卡活动的 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) and的想法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"));
}

但它不显示当前选项卡。它显示了我的默认选项卡 tab(0) .. 你能帮帮我吗?我的代码中还有什么要添加的吗?

4

1 回答 1

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"));
   }
}

现在它应该可以工作了。

于 2013-03-15T11:09:11.307 回答