prathameshr
prathameshr

Reputation: 59

Android- Tab content in the activity is lost

   public class SampleTabActivity extends TabActivity implements OnClickListener{static TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab
      //this is SampleTabActivity.java file
    intent = new Intent().setClass(this, TabOne.class);
    spec = tabHost.newTabSpec("tabOne");  
    spec.setContent(intent);  
    spec.setIndicator("Tab One");  
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
    LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
    android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
    RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
    TextView lf = (TextView) rllf.getChildAt(1);
    lf.setTextSize(20);

    intent = new Intent().setClass(this, TabTwo.class);
    spec = tabHost.newTabSpec("tabTwo");  
    spec.setContent(intent);  
    spec.setIndicator("Tab Two");
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
    RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
    TextView rf = (TextView) rlrf.getChildAt(1);
    rf.setTextSize(20);

    intent = new Intent().setClass(this, TabThree.class);
    spec = tabHost.newTabSpec("tabThree");  
    spec.setContent(intent);  
    spec.setIndicator("Tab Three");
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 40;
    RelativeLayout rlrp = (RelativeLayout) tw.getChildAt(2);
    TextView rp = (TextView) rlrp.getChildAt(1);
    rp.setTextSize(20);
    tabHost.setCurrentTab(0);
}}





public class TabOne extends ActivityGroup implements OnClickListener{   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tabone);
  TextView tv = new TextView(this);
  tv.setText("This is tab One");
  //this is TabOne.java file
  Button button = (Button)findViewById(R.id.button1);
  button.setOnClickListener(this);    
 }

@Override
public void onClick(View v) {   
    Intent intent = new Intent(this, TabTwo.class);         
    replaceContentView("TabTwo", intent);
    setTab(1);
}

public void replaceContentView(String id, Intent newIntent) {     
    View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
    this.setContentView(view);

    }

public void setTab(int index){    
    SampleTabActivityActivity ParentActivity;    
    ParentActivity = (SampleTabActivityActivity) this.getParent();    
    ParentActivity.getTabHost().setCurrentTab(index); 
    } }




public class TabTwo extends ActivityGroup implements OnClickListener{@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText("This is tab Two");
  setContentView(tv);
 }}


public class TabThree extends ActivityGroup implements OnClickListener{@Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText("This is tab Three");
  setContentView(R.layout.tabone);
 }}

Here, When I click continue button in TabOne...it goes to tab Two..and shows text "this is tab two"....but when I select tabOne by clicking on TabOne...the content is lost...

I cannot see Continue button again...instead I see " this is tab two "...

Please check code. please some one help...what need to be done...where I am going wrong...????

Upvotes: 0

Views: 721

Answers (2)

Tobias
Tobias

Reputation: 883

Your onClick method is hard coded, it will just go to tab two everytime you click your button.

Upvotes: 0

kosa
kosa

Reputation: 66637

Your code should be in onResume() method. onCreate() will be called if activity doesn't exists, subsequent calls will be processed by onResume() method. Refer activity life cycle in this link.

Upvotes: 1

Related Questions