diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index 7326170a6..6d13ce6a1 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -358,6 +358,15 @@
+
+
+
+
+
+
+
diff --git a/samples/ApiDemos/res/layout/fragment_arguments_fragment.xml b/samples/ApiDemos/res/layout/fragment_arguments_fragment.xml
new file mode 100644
index 000000000..b74823974
--- /dev/null
+++ b/samples/ApiDemos/res/layout/fragment_arguments_fragment.xml
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ApiDemos/res/layout/fragment_pager_support.xml b/samples/ApiDemos/res/layout/fragment_pager_support.xml
deleted file mode 100644
index a082e2e54..000000000
--- a/samples/ApiDemos/res/layout/fragment_pager_support.xml
+++ /dev/null
@@ -1,44 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/samples/ApiDemos/res/layout/fragment_stack.xml b/samples/ApiDemos/res/layout/fragment_stack.xml
index 1d12496ac..6cf2fe610 100644
--- a/samples/ApiDemos/res/layout/fragment_stack.xml
+++ b/samples/ApiDemos/res/layout/fragment_stack.xml
@@ -28,11 +28,19 @@
android:layout_weight="1">
-
-
+ android:layout_weight="0">
+
+
+
diff --git a/samples/ApiDemos/res/layout/fragment_tabs_fragment.xml b/samples/ApiDemos/res/layout/fragment_tabs_fragment.xml
new file mode 100644
index 000000000..89b275712
--- /dev/null
+++ b/samples/ApiDemos/res/layout/fragment_tabs_fragment.xml
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/Support13Demos/res/layout/hello_world.xml b/samples/ApiDemos/res/values-v17/bools.xml
similarity index 50%
rename from samples/Support13Demos/res/layout/hello_world.xml
rename to samples/ApiDemos/res/values-v17/bools.xml
index 433ea9bff..41bb2c3a4 100644
--- a/samples/Support13Demos/res/layout/hello_world.xml
+++ b/samples/ApiDemos/res/values-v17/bools.xml
@@ -1,5 +1,5 @@
-
-
-
-
-
+
+
+ true
+
diff --git a/samples/ApiDemos/res/values/bools.xml b/samples/ApiDemos/res/values/bools.xml
index c112fb482..38c602e74 100644
--- a/samples/ApiDemos/res/values/bools.xml
+++ b/samples/ApiDemos/res/values/bools.xml
@@ -34,4 +34,9 @@
for JellyBean is true. -->
false
+
+ false
+
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 778becaa3..64fc5ffce 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -124,6 +124,9 @@
From Attributes
Landscape Only
+ Demonstrates two embedded fragments
+ that are instantiated with arguments.
+
App/Fragment/Custom Animations
App/Fragment/Hide and Show
@@ -152,6 +155,8 @@
Show fragment 1 menu
Show fragment 2 menu
+ App/Fragment/Nesting Tabs
+
App/Fragment/Retain Instance
Current progress of retained fragment;
restarts if fragment is re-created.
@@ -160,8 +165,9 @@
App/Fragment/Receive Result
App/Fragment/Stack
- New fragment
-
+ Push
+ Pop
+
First
Last
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentArgumentsFragment.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentArgumentsFragment.java
new file mode 100644
index 000000000..7023c4380
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentArgumentsFragment.java
@@ -0,0 +1,53 @@
+/*
+ * 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.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * Demonstrates a fragment that can be configured through both Bundle arguments
+ * and layout attributes.
+ */
+public class FragmentArgumentsFragment extends Fragment {
+ @Override public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ if (savedInstanceState == null) {
+ // First-time init; create fragment to embed in activity.
+ FragmentTransaction ft = getChildFragmentManager().beginTransaction();
+ Fragment newFragment = FragmentArguments.MyFragment.newInstance("From Arguments 1");
+ ft.add(R.id.created1, newFragment);
+ newFragment = FragmentArguments.MyFragment.newInstance("From Arguments 2");
+ ft.add(R.id.created2, newFragment);
+ ft.commit();
+ }
+ }
+
+ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_arguments_fragment, container, false);
+ return v;
+ }
+//END_INCLUDE(create)
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java
index 6bc73e038..420e67f35 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentContextMenu.java
@@ -29,6 +29,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
+import android.widget.Toast;
/**
* Demonstration of displaying a context menu from a fragment.
@@ -65,10 +66,10 @@ public class FragmentContextMenu extends Activity {
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.a_item:
- Log.i("ContextMenu", "Item 1a was chosen");
+ Toast.makeText(getActivity(), "Item 1a was chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.b_item:
- Log.i("ContextMenu", "Item 1b was chosen");
+ Toast.makeText(getActivity(), "Item 1b was chosen", Toast.LENGTH_SHORT).show();
return true;
}
return super.onContextItemSelected(item);
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentMenuFragment.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentMenuFragment.java
new file mode 100644
index 000000000..6ac700355
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentMenuFragment.java
@@ -0,0 +1,95 @@
+/*
+ * 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.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Fragment;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.CheckBox;
+
+/**
+ * Demonstrates how fragments can participate in the options menu.
+ */
+public class FragmentMenuFragment extends Fragment {
+ Fragment mFragment1;
+ Fragment mFragment2;
+ CheckBox mCheckBox1;
+ CheckBox mCheckBox2;
+
+ // Update fragment visibility when check boxes are changed.
+ final OnClickListener mClickListener = new OnClickListener() {
+ public void onClick(View v) {
+ updateFragmentVisibility();
+ }
+ };
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_menu, container, false);
+
+ // Make sure the two menu fragments are created.
+ FragmentManager fm = getChildFragmentManager();
+ FragmentTransaction ft = fm.beginTransaction();
+ mFragment1 = fm.findFragmentByTag("f1");
+ if (mFragment1 == null) {
+ mFragment1 = new FragmentMenu.MenuFragment();
+ ft.add(mFragment1, "f1");
+ }
+ mFragment2 = fm.findFragmentByTag("f2");
+ if (mFragment2 == null) {
+ mFragment2 = new FragmentMenu.Menu2Fragment();
+ ft.add(mFragment2, "f2");
+ }
+ ft.commit();
+
+ // Watch check box clicks.
+ mCheckBox1 = (CheckBox)v.findViewById(R.id.menu1);
+ mCheckBox1.setOnClickListener(mClickListener);
+ mCheckBox2 = (CheckBox)v.findViewById(R.id.menu2);
+ mCheckBox2.setOnClickListener(mClickListener);
+
+ // Make sure fragments start out with correct visibility.
+ updateFragmentVisibility();
+
+ return v;
+ }
+
+ @Override
+ public void onViewStateRestored(Bundle savedInstanceState) {
+ super.onViewStateRestored(savedInstanceState);
+ // Make sure fragments are updated after check box view state is restored.
+ updateFragmentVisibility();
+ }
+
+ // Update fragment visibility based on current check box state.
+ void updateFragmentVisibility() {
+ FragmentTransaction ft = getChildFragmentManager().beginTransaction();
+ if (mCheckBox1.isChecked()) ft.show(mFragment1);
+ else ft.hide(mFragment1);
+ if (mCheckBox2.isChecked()) ft.show(mFragment2);
+ else ft.hide(mFragment2);
+ ft.commit();
+ }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentNestingTabs.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentNestingTabs.java
new file mode 100644
index 000000000..e35761216
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentNestingTabs.java
@@ -0,0 +1,116 @@
+/*
+ * 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.apis.app;
+
+//BEGIN_INCLUDE(complete)
+import android.app.ActionBar;
+import android.app.ActionBar.Tab;
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.os.Bundle;
+import android.widget.Toast;
+
+/**
+ * This demonstrates the use of action bar tabs and how they interact
+ * with other action bar features.
+ */
+public class FragmentNestingTabs extends Activity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ final ActionBar bar = getActionBar();
+ bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
+ bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
+
+ bar.addTab(bar.newTab()
+ .setText("Menus")
+ .setTabListener(new TabListener(
+ this, "menus", FragmentMenuFragment.class)));
+ bar.addTab(bar.newTab()
+ .setText("Args")
+ .setTabListener(new TabListener(
+ this, "args", FragmentArgumentsFragment.class)));
+ bar.addTab(bar.newTab()
+ .setText("Stack")
+ .setTabListener(new TabListener(
+ this, "stack", FragmentStackFragment.class)));
+ bar.addTab(bar.newTab()
+ .setText("Tabs")
+ .setTabListener(new TabListener(
+ this, "tabs", FragmentTabsFragment.class)));
+
+ if (savedInstanceState != null) {
+ bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
+ }
+ }
+
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
+ }
+
+ public static class TabListener implements ActionBar.TabListener {
+ private final Activity mActivity;
+ private final String mTag;
+ private final Class mClass;
+ private final Bundle mArgs;
+ private Fragment mFragment;
+
+ public TabListener(Activity activity, String tag, Class clz) {
+ this(activity, tag, clz, null);
+ }
+
+ public TabListener(Activity activity, String tag, Class clz, Bundle args) {
+ mActivity = activity;
+ mTag = tag;
+ mClass = clz;
+ mArgs = 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.
+ mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
+ if (mFragment != null && !mFragment.isDetached()) {
+ FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
+ ft.detach(mFragment);
+ ft.commit();
+ }
+ }
+
+ public void onTabSelected(Tab tab, FragmentTransaction ft) {
+ if (mFragment == null) {
+ mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
+ ft.add(android.R.id.content, mFragment, mTag);
+ } else {
+ ft.attach(mFragment);
+ }
+ }
+
+ public void onTabUnselected(Tab tab, FragmentTransaction ft) {
+ if (mFragment != null) {
+ ft.detach(mFragment);
+ }
+ }
+
+ public void onTabReselected(Tab tab, FragmentTransaction ft) {
+ Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
+ }
+ }
+}
+//END_INCLUDE(complete)
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
index 891eda486..242d67074 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
@@ -44,6 +44,12 @@ public class FragmentStack extends Activity {
addFragmentToStack();
}
});
+ button = (Button)findViewById(R.id.delete_fragment);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ getFragmentManager().popBackStack();
+ }
+ });
if (savedInstanceState == null) {
// Do first time initialization -- add initial fragment.
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentStackFragment.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentStackFragment.java
new file mode 100644
index 000000000..d33af64a8
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentStackFragment.java
@@ -0,0 +1,89 @@
+/*
+ * 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.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Fragment;
+import android.app.FragmentTransaction;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+
+public class FragmentStackFragment extends Fragment {
+ int mStackLevel = 1;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ if (savedInstanceState == null) {
+ // Do first time initialization -- add initial fragment.
+ Fragment newFragment = FragmentStack.CountingFragment.newInstance(mStackLevel);
+ FragmentTransaction ft = getChildFragmentManager().beginTransaction();
+ ft.add(R.id.simple_fragment, newFragment).commit();
+ } else {
+ mStackLevel = savedInstanceState.getInt("level");
+ }
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ View v = inflater.inflate(R.layout.fragment_stack, container, false);
+
+ // Watch for button clicks.
+ Button button = (Button)v.findViewById(R.id.new_fragment);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ addFragmentToStack();
+ }
+ });
+ button = (Button)v.findViewById(R.id.delete_fragment);
+ button.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ getChildFragmentManager().popBackStack();
+ }
+ });
+
+ return v;
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putInt("level", mStackLevel);
+ }
+
+ void addFragmentToStack() {
+ mStackLevel++;
+
+ // Instantiate a new fragment.
+ Fragment newFragment = FragmentStack.CountingFragment.newInstance(mStackLevel);
+
+ // Add the fragment to the activity, pushing this transaction
+ // on to the back stack.
+ FragmentTransaction ft = getChildFragmentManager().beginTransaction();
+ ft.replace(R.id.simple_fragment, newFragment);
+ ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
+ ft.addToBackStack(null);
+ ft.commit();
+ }
+}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabsFragment.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabsFragment.java
new file mode 100644
index 000000000..f1d8592e3
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabsFragment.java
@@ -0,0 +1,189 @@
+/*
+ * 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.apis.app;
+
+import java.util.HashMap;
+
+import com.example.android.apis.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("result").setIndicator("Result"),
+ FragmentReceiveResult.ReceiveResultFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
+ LoaderCursor.CursorLoaderListFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("apps").setIndicator("Apps"),
+ LoaderCustom.AppListFragment.class, null);
+ mTabManager.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
+ LoaderThrottle.ThrottledLoaderListFragment.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 mTabs = new HashMap();
+ 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)
diff --git a/samples/Support13Demos/AndroidManifest.xml b/samples/Support13Demos/AndroidManifest.xml
index bc32d099f..dfa4e640b 100644
--- a/samples/Support13Demos/AndroidManifest.xml
+++ b/samples/Support13Demos/AndroidManifest.xml
@@ -47,6 +47,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ApiDemos/res/layout/fragment_pager_support_list.xml b/samples/Support13Demos/res/layout/counting.xml
similarity index 53%
rename from samples/ApiDemos/res/layout/fragment_pager_support_list.xml
rename to samples/Support13Demos/res/layout/counting.xml
index bbe7b1d64..7d37c37cd 100644
--- a/samples/ApiDemos/res/layout/fragment_pager_support_list.xml
+++ b/samples/Support13Demos/res/layout/counting.xml
@@ -1,5 +1,5 @@
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
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 000000000..89b275712
--- /dev/null
+++ b/samples/Support13Demos/res/layout/fragment_tabs_fragment.xml
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/Support13Demos/res/values/strings.xml b/samples/Support13Demos/res/values/strings.xml
index 2fd12d496..92316d0ed 100644
--- a/samples/Support13Demos/res/values/strings.xml
+++ b/samples/Support13Demos/res/values/strings.xml
@@ -18,11 +18,17 @@
Support v13 Demos
Hello, World!
+ Retained state
+
Lorem ipsum dolor sit aie consectetur adipiscing\nPlloaso mako nuto
siwuf cakso dodtos anr koop.
+ Fragment/Nesting Pager
+
+ Fragment/Nesting State Pager
+
Fragment/Pager
First
Last
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 8672ed22a..d52955b21 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 @@ public class CountingFragment extends Fragment {
@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 000000000..0a88a0507
--- /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 mTabs = new ArrayList();
+
+ 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 = new ArrayList();
+
+ 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 = new HashMap();
+ 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)