我有一项活动,我有TabHost
和TabWidget
这样的活动:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:padding="5dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
我使用以下代码将三个标签及其各自的活动添加到TabHost
:
private void CreateTabs()
{
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String strJson = Intent.GetStringExtra (Constants.JSON_RESPONSE);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent (this, typeof (FirstActivity));
if (strJson != null)
{
intent.PutExtra (Constants.JSON_RESPONSE, strJson);
}
intent.AddFlags (ActivityFlags.NewTask);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = TabHost.NewTabSpec ("first");
spec.SetIndicator ("First", Resources.GetDrawable (Resource.Drawable.FirstIcon));
spec.SetContent (intent);
TabHost.AddTab (spec);
// Do the same for second tab
intent = new Intent (this, typeof (SecondActivity));
if (strJson != null)
{
intent.PutExtra (Constants.JSON_RESPONSE, strJson);
}
intent.AddFlags (ActivityFlags.NewTask);
spec = TabHost.NewTabSpec ("second");
spec.SetIndicator ("Second", Resources.GetDrawable (Resource.Drawable.SecondIcon));
spec.SetContent (intent);
TabHost.AddTab (spec);
// Do the same for third tab
intent = new Intent (this, typeof (ThirdActivity));
if (strJson != null)
{
intent.PutExtra (Constants.JSON_RESPONSE, strJson);
}
intent.AddFlags (ActivityFlags.NewTask);
spec = TabHost.NewTabSpec ("third");
spec.SetIndicator ("Third", Resources.GetDrawable (Resource.Drawable.third_icon));
spec.SetContent (intent);
TabHost.AddTab (spec);
//Set the tab you want to show
TabHost.CurrentTab = 1;
}
正如您所看到的,在最后一行,当我打开此标签式活动时,我正在显示第二个标签,这就是它的功能,但实际上它首先调用了OnCreate
FirstActivity
然后在显示OnCreate
之前调用SecondActivity
的{{1}}。 (仅当我点击第三个标签以实际看到该活动时,才会调用SecondActivity
的{{1}}。这绝对没问题。
我希望仅在点击第一个标签时调用OnCreate
ThirdActivity
,因为:
OnCreate
获取了用户的当前位置,如果我在用户实际导航到所需的屏幕之前得到它,那么如果用户已经更改了他的位置,则可能不正确。请告诉我如何实现这一目标?