2012-09-13 90 views
2

您好我有一個Android應用程序的問題我正在構建應用程序使用選項卡一旦選項卡被點擊它開始一個活動如果我旋轉屏幕應用程序返回到默認選項卡。我想旋轉目前的標籤保留在這裏是我的代碼,我已經得到了我看到了類似的問題,但在這方面的解決方案不能幫助我,所以請一個例子會真的幫助我當屏幕旋轉時,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; 

} 
} 

回答

3

當你的應用程序的方向變化,它再次調用onCreate方法。當用戶更改標籤頁時,您可以將標籤索引添加到savedInstanceState變量標籤。 然後在oncreate中,您可以檢查您的tabindex是否已設置,如果是,則顯示正確的選項卡。

+0

你能幫我一些代碼示例 – user1534409

+0

看看這個鏈接 http://stackoverflow.com/questions/151777/saving-activity-state-in-android – ePeace

+0

謝謝你這幫助我很多讓我問這個問題的另一個問題,你怎麼樣確保輪換後,子活動仍然在標籤 – user1534409

1

這可能會幫助你

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

     if your android:targetSdkVersion="13" or more 
    android:configChanges="orientation|keyboardHidden|screenSize"> 
+0

我有android:targetSdkVersion =「15」,但第二個解決方案不工作,它帶來了清單中的錯誤,第一個解決方案不會改變我的問題我可以做什麼錯誤 – user1534409

相關問題