API demo for nesting fragments.
Change-Id: I7f20e4f00cf68c78c578ae620a95de7fa97f0761
diff --git a/samples/Support13Demos/AndroidManifest.xml b/samples/Support13Demos/AndroidManifest.xml
index bc32d09..dfa4e64 100644
--- a/samples/Support13Demos/AndroidManifest.xml
+++ b/samples/Support13Demos/AndroidManifest.xml
@@ -47,6 +47,22 @@
<!-- Fragment Support Samples -->
+ <activity android:name=".app.FragmentNestingPagerSupport"
+ android:label="@string/fragment_nesting_pager_support">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="com.example.android.supportv13.SUPPORT13_SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
+ <activity android:name=".app.FragmentNestingStatePagerSupport"
+ android:label="@string/fragment_nesting_state_pager_support">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="com.example.android.supportv13.SUPPORT13_SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
<activity android:name=".app.FragmentPagerSupport"
android:label="@string/fragment_pager_support">
<intent-filter>
diff --git a/samples/Support13Demos/res/layout/counting.xml b/samples/Support13Demos/res/layout/counting.xml
new file mode 100644
index 0000000..7d37c37
--- /dev/null
+++ b/samples/Support13Demos/res/layout/counting.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2007 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!-- Demonstrates basic application screen.
+ See corresponding Java code com.android.sdk.app.HelloWorld.java. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@android:drawable/gallery_thumb">
+ <TextView android:id="@+id/text"
+ android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:gravity="center_vertical|center_horizontal"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:text="@string/hello_world"/>
+ <CheckBox android:id="@+id/menu1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:checked="true"
+ android:text="@string/retained">
+ </CheckBox>
+</LinearLayout>
diff --git a/samples/Support13Demos/res/layout/fragment_tabs_fragment.xml b/samples/Support13Demos/res/layout/fragment_tabs_fragment.xml
new file mode 100644
index 0000000..89b2757
--- /dev/null
+++ b/samples/Support13Demos/res/layout/fragment_tabs_fragment.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/* Copyright 2012, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+** http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<TabHost
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@android:id/tabhost"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <LinearLayout
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <TabWidget
+ android:id="@android:id/tabs"
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_weight="0"/>
+
+ <FrameLayout
+ android:id="@android:id/tabcontent"
+ android:layout_width="0dp"
+ android:layout_height="0dp"
+ android:layout_weight="0"/>
+
+ <FrameLayout
+ android:id="@+android:id/realtabcontent"
+ android:layout_width="match_parent"
+ android:layout_height="0dp"
+ android:layout_weight="1"/>
+
+ </LinearLayout>
+</TabHost>
diff --git a/samples/Support13Demos/res/layout/hello_world.xml b/samples/Support13Demos/res/layout/hello_world.xml
deleted file mode 100644
index 433ea9b..0000000
--- a/samples/Support13Demos/res/layout/hello_world.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2007 The Android Open Source Project
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--->
-
-<!-- Demonstrates basic application screen.
- See corresponding Java code com.android.sdk.app.HelloWorld.java. -->
-
-<!-- This screen consists of a single text field that
- displays our "Hello, World!" text. -->
-<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:gravity="center_vertical|center_horizontal"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:text="@string/hello_world"/>
diff --git a/samples/Support13Demos/res/values/strings.xml b/samples/Support13Demos/res/values/strings.xml
index 2fd12d4..92316d0 100644
--- a/samples/Support13Demos/res/values/strings.xml
+++ b/samples/Support13Demos/res/values/strings.xml
@@ -18,11 +18,17 @@
<string name="activity_sample_code">Support v13 Demos</string>
<string name="hello_world"><b>Hello, <i>World!</i></b></string>
+ <string name="retained">Retained state</string>
+
<string name="alert_dialog_two_buttons_title">
Lorem ipsum dolor sit aie consectetur adipiscing\nPlloaso mako nuto
siwuf cakso dodtos anr koop.
</string>
+ <string name="fragment_nesting_pager_support">Fragment/Nesting Pager</string>
+
+ <string name="fragment_nesting_state_pager_support">Fragment/Nesting State Pager</string>
+
<string name="fragment_pager_support">Fragment/Pager</string>
<string name="first">First</string>
<string name="last">Last</string>
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java b/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java
index 8672ed2..d52955b 100644
--- a/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/CountingFragment.java
@@ -58,10 +58,9 @@
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.hello_world, container, false);
+ View v = inflater.inflate(R.layout.counting, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
- tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java
new file mode 100644
index 0000000..0a88a05
--- /dev/null
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingPagerSupport.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.supportv13.app;
+
+import java.util.ArrayList;
+
+import com.example.android.supportv13.R;
+
+import android.app.ActionBar;
+import android.app.ActionBar.Tab;
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v13.app.FragmentPagerAdapter;
+import android.support.v4.view.ViewPager;
+
+//BEGIN_INCLUDE(complete)
+public class FragmentNestingPagerSupport extends Activity {
+ ViewPager mViewPager;
+ TabsAdapter mTabsAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mViewPager = new ViewPager(this);
+ mViewPager.setId(R.id.pager);
+ setContentView(mViewPager);
+
+ final ActionBar bar = getActionBar();
+ bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+ bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
+
+ mTabsAdapter = new TabsAdapter(this, mViewPager);
+ mTabsAdapter.addTab(bar.newTab().setText("Simple"),
+ CountingFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("List"),
+ FragmentPagerSupport.ArrayListFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Cursor"),
+ CursorFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Tabs"),
+ FragmentTabsFragment.class, null);
+
+ if (savedInstanceState != null) {
+ bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
+ }
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
+ }
+
+ /**
+ * This is a helper class that implements the management of tabs and all
+ * details of connecting a ViewPager with associated TabHost. It relies on a
+ * trick. Normally a tab host has a simple API for supplying a View or
+ * Intent that each tab will show. This is not sufficient for switching
+ * between pages. So instead we make the content part of the tab host
+ * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
+ * view to show as the tab content. It listens to changes in tabs, and takes
+ * care of switch to the correct paged in the ViewPager whenever the selected
+ * tab changes.
+ */
+ public static class TabsAdapter extends FragmentPagerAdapter
+ implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
+ private final Context mContext;
+ private final ActionBar mActionBar;
+ private final ViewPager mViewPager;
+ private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
+
+ static final class TabInfo {
+ private final Class<?> clss;
+ private final Bundle args;
+
+ TabInfo(Class<?> _class, Bundle _args) {
+ clss = _class;
+ args = _args;
+ }
+ }
+
+ public TabsAdapter(Activity activity, ViewPager pager) {
+ super(activity.getFragmentManager());
+ mContext = activity;
+ mActionBar = activity.getActionBar();
+ mViewPager = pager;
+ mViewPager.setAdapter(this);
+ mViewPager.setOnPageChangeListener(this);
+ }
+
+ public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
+ TabInfo info = new TabInfo(clss, args);
+ tab.setTag(info);
+ tab.setTabListener(this);
+ mTabs.add(info);
+ mActionBar.addTab(tab);
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getCount() {
+ return mTabs.size();
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ TabInfo info = mTabs.get(position);
+ return Fragment.instantiate(mContext, info.clss.getName(), info.args);
+ }
+
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ mActionBar.setSelectedNavigationItem(position);
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+ }
+
+ @Override
+ public void onTabSelected(Tab tab, FragmentTransaction ft) {
+ Object tag = tab.getTag();
+ for (int i=0; i<mTabs.size(); i++) {
+ if (mTabs.get(i) == tag) {
+ mViewPager.setCurrentItem(i);
+ }
+ }
+ }
+
+ @Override
+ public void onTabUnselected(Tab tab, FragmentTransaction ft) {
+ }
+
+ @Override
+ public void onTabReselected(Tab tab, FragmentTransaction ft) {
+ }
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java
new file mode 100644
index 0000000..5863852
--- /dev/null
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentNestingStatePagerSupport.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.supportv13.app;
+
+import java.util.ArrayList;
+
+import com.example.android.supportv13.R;
+
+import android.app.ActionBar;
+import android.app.ActionBar.Tab;
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v13.app.FragmentStatePagerAdapter;
+import android.support.v4.view.ViewPager;
+
+//BEGIN_INCLUDE(complete)
+public class FragmentNestingStatePagerSupport extends Activity {
+ ViewPager mViewPager;
+ TabsAdapter mTabsAdapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mViewPager = new ViewPager(this);
+ mViewPager.setId(R.id.pager);
+ setContentView(mViewPager);
+
+ final ActionBar bar = getActionBar();
+ bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+ bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
+
+ mTabsAdapter = new TabsAdapter(this, mViewPager);
+ mTabsAdapter.addTab(bar.newTab().setText("Simple"),
+ CountingFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("List"),
+ FragmentPagerSupport.ArrayListFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Cursor"),
+ CursorFragment.class, null);
+ mTabsAdapter.addTab(bar.newTab().setText("Tabs"),
+ FragmentTabsFragment.class, null);
+
+ if (savedInstanceState != null) {
+ bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
+ }
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
+ }
+
+ /**
+ * This is a helper class that implements the management of tabs and all
+ * details of connecting a ViewPager with associated TabHost. It relies on a
+ * trick. Normally a tab host has a simple API for supplying a View or
+ * Intent that each tab will show. This is not sufficient for switching
+ * between pages. So instead we make the content part of the tab host
+ * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
+ * view to show as the tab content. It listens to changes in tabs, and takes
+ * care of switch to the correct paged in the ViewPager whenever the selected
+ * tab changes.
+ */
+ public static class TabsAdapter extends FragmentStatePagerAdapter
+ implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
+ private final Context mContext;
+ private final ActionBar mActionBar;
+ private final ViewPager mViewPager;
+ private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
+
+ static final class TabInfo {
+ private final Class<?> clss;
+ private final Bundle args;
+
+ TabInfo(Class<?> _class, Bundle _args) {
+ clss = _class;
+ args = _args;
+ }
+ }
+
+ public TabsAdapter(Activity activity, ViewPager pager) {
+ super(activity.getFragmentManager());
+ mContext = activity;
+ mActionBar = activity.getActionBar();
+ mViewPager = pager;
+ mViewPager.setAdapter(this);
+ mViewPager.setOnPageChangeListener(this);
+ }
+
+ public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
+ TabInfo info = new TabInfo(clss, args);
+ tab.setTag(info);
+ tab.setTabListener(this);
+ mTabs.add(info);
+ mActionBar.addTab(tab);
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getCount() {
+ return mTabs.size();
+ }
+
+ @Override
+ public Fragment getItem(int position) {
+ TabInfo info = mTabs.get(position);
+ return Fragment.instantiate(mContext, info.clss.getName(), info.args);
+ }
+
+ @Override
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+ }
+
+ @Override
+ public void onPageSelected(int position) {
+ mActionBar.setSelectedNavigationItem(position);
+ }
+
+ @Override
+ public void onPageScrollStateChanged(int state) {
+ }
+
+ @Override
+ public void onTabSelected(Tab tab, FragmentTransaction ft) {
+ Object tag = tab.getTag();
+ for (int i=0; i<mTabs.size(); i++) {
+ if (mTabs.get(i) == tag) {
+ mViewPager.setCurrentItem(i);
+ }
+ }
+ }
+
+ @Override
+ public void onTabUnselected(Tab tab, FragmentTransaction ft) {
+ }
+
+ @Override
+ public void onTabReselected(Tab tab, FragmentTransaction ft) {
+ }
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java
new file mode 100644
index 0000000..fbc464d
--- /dev/null
+++ b/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentTabsFragment.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.example.android.supportv13.app;
+
+import java.util.HashMap;
+
+import com.example.android.supportv13.R;
+
+import android.app.Fragment;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.content.Context;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TabHost;
+
+public class FragmentTabsFragment extends Fragment {
+ TabHost mTabHost;
+ TabManager mTabManager;
+ String mCurrentTabTag;
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_tabs_fragment, container, false);
+ mTabHost = (TabHost)v.findViewById(android.R.id.tabhost);
+ mTabHost.setup();
+
+ mTabManager = new TabManager(getActivity(), getChildFragmentManager(),
+ mTabHost, R.id.realtabcontent);
+
+ mTabManager.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
+ CountingFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("array").setIndicator("Array"),
+ FragmentPagerSupport.ArrayListFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("cursor").setIndicator("Cursor"),
+ CursorFragment.class, null);
+
+ return v;
+ }
+
+ @Override
+ public void onViewStateRestored(Bundle savedInstanceState) {
+ super.onViewStateRestored(savedInstanceState);
+ if (savedInstanceState != null) {
+ mCurrentTabTag = savedInstanceState.getString("tab");
+ }
+ mTabHost.setCurrentTabByTag(mCurrentTabTag);
+ }
+
+ @Override
+ public void onDestroyView() {
+ super.onDestroyView();
+ // Need to remember the selected tab so that we can restore it if
+ // we later re-create the views.
+ mCurrentTabTag = mTabHost.getCurrentTabTag();
+ mTabHost = null;
+ mTabManager = null;
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putString("tab", mTabHost != null
+ ? mTabHost.getCurrentTabTag() : mCurrentTabTag);
+ }
+
+ /**
+ * This is a helper class that implements a generic mechanism for
+ * associating fragments with the tabs in a tab host. It relies on a
+ * trick. Normally a tab host has a simple API for supplying a View or
+ * Intent that each tab will show. This is not sufficient for switching
+ * between fragments. So instead we make the content part of the tab host
+ * 0dp high (it is not shown) and the TabManager supplies its own dummy
+ * view to show as the tab content. It listens to changes in tabs, and takes
+ * care of switch to the correct fragment shown in a separate content area
+ * whenever the selected tab changes.
+ */
+ public static class TabManager implements TabHost.OnTabChangeListener {
+ private final Context mContext;
+ private final FragmentManager mManager;
+ private final TabHost mTabHost;
+ private final int mContainerId;
+ private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
+ TabInfo mLastTab;
+
+ static final class TabInfo {
+ private final String tag;
+ private final Class<?> clss;
+ private final Bundle args;
+ private Fragment fragment;
+
+ TabInfo(String _tag, Class<?> _class, Bundle _args) {
+ tag = _tag;
+ clss = _class;
+ args = _args;
+ }
+ }
+
+ static class DummyTabFactory implements TabHost.TabContentFactory {
+ private final Context mContext;
+
+ public DummyTabFactory(Context context) {
+ mContext = context;
+ }
+
+ @Override
+ public View createTabContent(String tag) {
+ View v = new View(mContext);
+ v.setMinimumWidth(0);
+ v.setMinimumHeight(0);
+ return v;
+ }
+ }
+
+ public TabManager(Context context, FragmentManager manager, TabHost tabHost,
+ int containerId) {
+ mContext = context;
+ mManager = manager;
+ mTabHost = tabHost;
+ mContainerId = containerId;
+ mTabHost.setOnTabChangedListener(this);
+ }
+
+ public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
+ tabSpec.setContent(new DummyTabFactory(mContext));
+ String tag = tabSpec.getTag();
+
+ TabInfo info = new TabInfo(tag, clss, args);
+
+ // Check to see if we already have a fragment for this tab, probably
+ // from a previously saved state. If so, deactivate it, because our
+ // initial state is that a tab isn't shown.
+ info.fragment = mManager.findFragmentByTag(tag);
+ if (info.fragment != null && !info.fragment.isDetached()) {
+ FragmentTransaction ft = mManager.beginTransaction();
+ ft.detach(info.fragment);
+ ft.commit();
+ }
+
+ mTabs.put(tag, info);
+ mTabHost.addTab(tabSpec);
+ }
+
+ @Override
+ public void onTabChanged(String tabId) {
+ TabInfo newTab = mTabs.get(tabId);
+ if (mLastTab != newTab) {
+ FragmentTransaction ft = mManager.beginTransaction();
+ if (mLastTab != null) {
+ if (mLastTab.fragment != null) {
+ ft.detach(mLastTab.fragment);
+ }
+ }
+ if (newTab != null) {
+ if (newTab.fragment == null) {
+ newTab.fragment = Fragment.instantiate(mContext,
+ newTab.clss.getName(), newTab.args);
+ ft.add(mContainerId, newTab.fragment, newTab.tag);
+ } else {
+ ft.attach(newTab.fragment);
+ }
+ }
+
+ mLastTab = newTab;
+ ft.commit();
+ mManager.executePendingTransactions();
+ }
+ }
+ }
+}
+//END_INCLUDE(complete)