简体   繁体   中英

Android Eclipse, two activities in two different tabs, keeps calling onCreate()!

I have 2 activities, let's say Activity1 and Activity2. I have added these 2 into 2 separate tabs under a TabHost.

Every time I press the required tab to view the contents, the onCreate() for each activity is called and therefore restarts the activity! Why is that? How can I prevent this from happening??

Thanks.

Code from the TabHostActivity class:

package zt.ztactive;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

 public class TabHostActivity extends TabActivity { 

     TabHost tabHost;

     /** Called when the activity is first created. */

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

         /** TabHost will have Tabs */ 
         tabHost = (TabHost)findViewById(android.R.id.tabhost); 

         /** TabSpec used to create a new tab.  
          * By using TabSpec only we can able to setContent to the tab.  
          * By using TabSpec setIndicator() we can set name to tab. */

         /** tid1 is firstTabSpec Id. Its used to access outside. */ 
         TabSpec firstTabSpec = tabHost.newTabSpec("tid1");  
         TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 

         /** TabSpec setIndicator() is used to set name for the tab. */ 
         /** TabSpec setContent() is used to set content for a particular tab. */ 
         firstTabSpec.setIndicator("First Tab Name").setContent(new Intent(this,Activity1.class));  
         secondTabSpec.setIndicator("Second Tab Name").setContent(new Intent(this,Activity2.class)); 

         /** Add tabSpec to the TabHost to display. */ 
         tabHost.addTab(firstTabSpec);  
         tabHost.addTab(secondTabSpec);  
     }

 }

Can you show some code as to how you are using Activities in your tabhost. Ideally once the tab is created, the activities would call onResume and not onCreate as the activities are not destroyed when you move from one tab to another.

replace this TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); with:

TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");

To preserve memory as few activities as possible are loaded at one point. As an activity is not visible when the tab is not shown, the activity is destroyed.

Either you shouldn't create an activity for each tab but instead have different views for each tab in the same activity, or you should save the state of the activity for when it is loaded again.

There's a small example on how to make a tabhost without activities here: http://dewful.com/?p=15

The problem for me was the same to what PravinCG describes above. Making sure that the tags in all TabSpecs are unique, easily solved the problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM