2

嗨,我有一个 Android 应用程序的问题,我正在构建应用程序使用选项卡,一旦单击选项卡,它就会启动一个活动,如果我旋转屏幕,应用程序返回默认选项卡,就会出现问题。我希望在轮换时保留当前选项卡,这是我的代码,我在哪里看到了类似的问题,但是那里的解决方案无法帮助我,所以请举个例子真的对我有帮助

    import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MultiLingualDictionaryActivity extends Activity   {
    /** Called when the activity is first created. */
    private String TAB_TAG,TAB_TAG2,TAB_TAG3;
    TabHost tabHost;
    LocalActivityManager mlam;  
    private int CurrentTab = 1 ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main_tabs);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        TAB_TAG = this.getString(R.string.search);
        TAB_TAG2 = this.getString(R.string.more);
        TAB_TAG3 = this.getString(R.string.history);
         mlam = new LocalActivityManager(this, false);
         tabHost = (TabHost) findViewById(R.id.tabhost);
         mlam.dispatchCreate(savedInstanceState);
         tabHost.setup(mlam);

         TabSpec spec1=tabHost.newTabSpec(TAB_TAG);
         spec1.setIndicator(TAB_TAG,getResources().getDrawable(R.drawable.search));
         Intent in1=new Intent(this, ManageTab.class);
         spec1.setContent(in1);

         TabSpec spec4=tabHost.newTabSpec(TAB_TAG2);
         spec4.setIndicator(TAB_TAG2,getResources().getDrawable(R.drawable.more));
         Intent in4=new Intent(this,MoreActivity.class);
         spec4.setContent(in4);

         tabHost.addTab(spec1);
         tabHost.addTab(tabHost.newTabSpec(TAB_TAG3)
                .setIndicator(TAB_TAG3,getResources().getDrawable(R.drawable.history))
                .setContent(new Intent(this, ManageTab2.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
         tabHost.addTab(spec4);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        getLastNonConfigurationInstance(); 
         super.onCreate(savedInstanceState);  

        tabHost.setCurrentTab(CurrentTab);
        tabHost.setOnTabChangedListener(tabhostListener);
    }
    TabHost.OnTabChangeListener tabhostListener =  new TabHost.OnTabChangeListener(){
        public void onTabChanged(String tabId) {
            if(TAB_TAG.equals(tabId)) {

                CurrentTab = tabHost.getCurrentTab();
                System.out.println(CurrentTab);
            }
            else if(TAB_TAG2.equals(tabId)) {
                CurrentTab = tabHost.getCurrentTab();
                System.out.println(CurrentTab);
            }
            else{
                CurrentTab = tabHost.getCurrentTab();
                System.out.println(CurrentTab);
            //mlam.destroyActivity(TAB_TAG3, true);
             //   mlam.startActivity(TAB_TAG3, new Intent(getApplicationContext(),ManageTab2.class));
            //System.out.println("destroy tab2");
        }
    }
};

/*@Override
protected void onStart(){
    tabHost.setCurrentTab(CurrentTab);
}*/


@Override
public Object onRetainNonConfigurationInstance(){
    CurrentTab = tabHost.getCurrentTab();
        return CurrentTab;

}
}
4

2 回答 2

3

当您的应用程序的方向发生变化时,它会再次调用 onCreate 方法。您可以做的是在用户更改选项卡时将选项卡索引添加到您的 savedInstanceState 变量。然后在 oncreate 中,您可以检查您的 tabindex 是否已设置,如果是,则显示正确的选项卡。

于 2012-09-13T12:31:05.553 回答
1

这可能会帮助你

        if your android:targetSdkVersion="12" or less
     android:configChanges="orientation|keyboardHidden">

      if your  android:targetSdkVersion="13" or more
  android:configChanges="orientation|keyboardHidden|screenSize">
于 2012-09-13T12:15:30.240 回答