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 に答える