看來,當我點擊我的選項卡按鈕時,onCreate方法被調用兩次。有沒有另一種方法來製作一個tabhost?繼承人我的代碼:Tabhost選項卡onCreate方法被調用兩次
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>
好的,這很奇怪。今天,onCreate方法只被調用一次?昨天的日誌顯示,onCreate被稱爲兩次... –