Tabs 即使将第二个选项卡定义为CurrentTab,也会调用第一个选项卡的OnCreate()
我有一个活动,上面有一个Tabs 即使将第二个选项卡定义为CurrentTab,也会调用第一个选项卡的OnCreate(),tabs,xamarin.android,android-tabhost,oncreate,tabwidget,Tabs,Xamarin.android,Android Tabhost,Oncreate,Tabwidget,我有一个活动,上面有一个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="fi
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>
正如您所看到的,在最后一行,当我打开这个选项卡式活动时,我正在创建第二个选项卡,这就是它所做的,但实际上它首先调用第一个活动的OnCreate
,然后在显示第二个活动之前调用OnCreate
的OnCreate
。(只有当我点击第三个选项卡以实际查看该活动时,才会调用第三个活动的OnCreate
,这是绝对正确的)
我希望仅在点击第一个选项卡时调用FirstActivity
的OnCreate
,因为:
应用程序执行一些在该实例中不需要的工作,因此使用了不必要的资源
更重要的是,我的FirstActivity
获取用户的当前位置,如果我在用户实际导航到所需屏幕之前获取该位置,那么如果用户已经更改了位置,则该位置可能不正确
<>请让我知道如何实现这一点吗?< /P>你最好把逻辑移到OnReSuMe()吗?这正是我现在正在做的,但我认为它是解决办法而不是解决办法!因此,我们正在寻找解决方案。
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;
}