Tabhost选项卡onCreate方法被调用两次

时间:2011-09-15 14:07:17

标签: android tabs android-tabhost

当我点击我的标签按钮时,onCreate方法会被调用两次。还有另一种制作tabhost的方法吗?继承我的代码:

public class MyTabHostActivity extends TabActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabactivity);

    TabHost tabHost=getTabHost();
    setup(tabHost);       
}


private void setup(TabHost tabHost)
{
    Intent intent1 = new Intent(this, Activity1.class);
    setupTab("Activity1", tabHost, intent1);

    Intent intent2 = new Intent(this,Activity2.class);
    setupTab("Activity2", tabHost, intent2); 

    Intent intent3 = new Intent(this,Activity3.class);
    setupTab("Activity3", tabHost, intent3);    
}

private void setupTab(final String tag, final TabHost mTabHost, Intent intent) {
    View tabview = createTabView(mTabHost.getContext(), tag, mTabHost);
    TabSpec setContent =  mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
    mTabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text, final  TabHost mTabHost) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}
}

public class Activity1 extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("Activity1", "onCreate");

}

}

public class Activity2 extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("Activity2", "onCreate");

}

}
public class Activity3 extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("Activity3", "onCreate");

}

}

tabactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android">


<TabWidget android:layout_width="fill_parent" android:layout_gravity="bottom"
    android:layout_height="wrap_content" android:id="@android:id/tabs" />

<FrameLayout android:layout_width="fill_parent" android:paddingBottom="45dp"
    android:layout_height="wrap_content" android:id="@android:id/tabcontent">
</FrameLayout>
</TabHost>

tab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:padding="10dip" android:gravity="center" android:orientation="vertical">

<TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15dip"  />
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
       >
<TextView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/hello"
 />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.test"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MyTabHostActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Activity1" android:screenOrientation="portrait" />
    <activity android:name=".Activity2" android:screenOrientation="portrait" />
    <activity android:name=".Activity3" android:screenOrientation="portrait" />
</application>
</manifest>

1 个答案:

答案 0 :(得分:-2)

它不会被调用两次。我很确定你在emulator中运行代码,并且看到方法被触发了两次。但这只是<{1}}中的另一个错误,这在真实设备上永远不会发生。