Reputation: 325
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;
}
}
Upvotes: 2
Views: 2470
Reputation: 1997
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.
Upvotes: 3
Reputation: 1698
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">
Upvotes: 1