2

Hi i have an issue with an android app i'm building the app uses tabs once a tab is clicked it starts an activity the problem arises if i rotate the screen the app goes back to the default tab. I would like on rotate for the current tab to be retained here is my code and where i've gotten i saw a similar question on this but the solutions there were not able t help me so please an example would really help me

    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;

}
}
Gan
  • 1,349
  • 2
  • 10
  • 27
user1534409
  • 325
  • 8
  • 21

2 Answers2

3

When the orientation of your application changes, it calls the onCreate method again. What you can do is add the tab index to your savedInstanceState variabel when your user changes tab. Then in oncreate you can check if your tabindex is set, if so, show the correct tab.

ePeace
  • 1,987
  • 1
  • 17
  • 23
  • check out this link http://stackoverflow.com/questions/151777/saving-activity-state-in-android – ePeace Sep 13 '12 at 12:45
  • thanks man this helped me a lot allow me to ask another question in regards to this how do you ensure that after rotation the child activities remains on the tab – user1534409 Sep 13 '12 at 13:17
  • I believe tabHost.setCurrentTab should work? Although I doubt the child activities would retain their state. – ePeace Sep 13 '12 at 13:26
1

This may help you

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

      if your  android:targetSdkVersion="13" or more
  android:configChanges="orientation|keyboardHidden|screenSize">
Rajendra
  • 1,700
  • 14
  • 17
  • i have android:targetSdkVersion="15" but the second solution is not working it brings an error in the manifest and the first solution offers no change to my problem what could i be doing wrong – user1534409 Sep 13 '12 at 12:22