The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.os.Bundle; |
| 20 | import android.view.View; |
| 21 | import android.widget.TabHost; |
| 22 | import android.widget.TabWidget; |
| 23 | import android.widget.TextView; |
| 24 | |
| 25 | /** |
Dianne Hackborn | 2f04883 | 2011-06-16 13:31:57 -0700 | [diff] [blame] | 26 | * <p>For apps developing against {@link android.os.Build.VERSION_CODES#HONEYCOMB} |
| 27 | * or later, tabs are typically presented in the UI using the new |
| 28 | * {@link ActionBar#newTab() ActionBar.newTab()} and |
| 29 | * related APIs for placing tabs within their action bar area.</p> |
| 30 | * |
| 31 | * <p>A replacement for TabActivity can also be implemented by directly using |
| 32 | * TabHost. You will need to define a layout that correctly uses a TabHost |
| 33 | * with a TabWidget as well as an area in which to display your tab content. |
| 34 | * A typical example would be:</p> |
| 35 | * |
| 36 | * {@sample development/samples/Support4Demos/res/layout/fragment_tabs.xml complete} |
| 37 | * |
| 38 | * <p>The implementation needs to take over responsibility for switching |
| 39 | * the shown content when the user switches between tabs. |
| 40 | * |
| 41 | * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.java |
| 42 | * complete} |
| 43 | * |
| 44 | * <p>Also see the <a href="{@docRoot}resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html"> |
| 45 | * Fragment Tabs Pager</a> sample for an example of using the support library's ViewPager to |
| 46 | * allow the user to swipe the content to switch between tabs.</p> |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 47 | * |
| 48 | * @deprecated New applications should use Fragments instead of this class; |
| 49 | * to continue to run on older devices, you can use the v4 support library |
| 50 | * which provides a version of the Fragment API that is compatible down to |
| 51 | * {@link android.os.Build.VERSION_CODES#DONUT}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | */ |
Dianne Hackborn | 2f04883 | 2011-06-16 13:31:57 -0700 | [diff] [blame] | 53 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | public class TabActivity extends ActivityGroup { |
| 55 | private TabHost mTabHost; |
| 56 | private String mDefaultTab = null; |
| 57 | private int mDefaultTabIndex = -1; |
| 58 | |
| 59 | public TabActivity() { |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Sets the default tab that is the first tab highlighted. |
| 64 | * |
| 65 | * @param tag the name of the default tab |
| 66 | */ |
| 67 | public void setDefaultTab(String tag) { |
| 68 | mDefaultTab = tag; |
| 69 | mDefaultTabIndex = -1; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the default tab that is the first tab highlighted. |
| 74 | * |
| 75 | * @param index the index of the default tab |
| 76 | */ |
| 77 | public void setDefaultTab(int index) { |
| 78 | mDefaultTab = null; |
| 79 | mDefaultTabIndex = index; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void onRestoreInstanceState(Bundle state) { |
| 84 | super.onRestoreInstanceState(state); |
| 85 | ensureTabHost(); |
| 86 | String cur = state.getString("currentTab"); |
| 87 | if (cur != null) { |
| 88 | mTabHost.setCurrentTabByTag(cur); |
| 89 | } |
| 90 | if (mTabHost.getCurrentTab() < 0) { |
| 91 | if (mDefaultTab != null) { |
| 92 | mTabHost.setCurrentTabByTag(mDefaultTab); |
| 93 | } else if (mDefaultTabIndex >= 0) { |
| 94 | mTabHost.setCurrentTab(mDefaultTabIndex); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected void onPostCreate(Bundle icicle) { |
| 101 | super.onPostCreate(icicle); |
| 102 | |
| 103 | ensureTabHost(); |
| 104 | |
| 105 | if (mTabHost.getCurrentTab() == -1) { |
| 106 | mTabHost.setCurrentTab(0); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onSaveInstanceState(Bundle outState) { |
| 112 | super.onSaveInstanceState(outState); |
| 113 | String currentTabTag = mTabHost.getCurrentTabTag(); |
| 114 | if (currentTabTag != null) { |
| 115 | outState.putString("currentTab", currentTabTag); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Updates the screen state (current list and other views) when the |
| 121 | * content changes. |
| 122 | * |
| 123 | *@see Activity#onContentChanged() |
| 124 | */ |
| 125 | @Override |
| 126 | public void onContentChanged() { |
| 127 | super.onContentChanged(); |
| 128 | mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost); |
| 129 | |
| 130 | if (mTabHost == null) { |
| 131 | throw new RuntimeException( |
| 132 | "Your content must have a TabHost whose id attribute is " + |
| 133 | "'android.R.id.tabhost'"); |
| 134 | } |
| 135 | mTabHost.setup(getLocalActivityManager()); |
| 136 | } |
| 137 | |
| 138 | private void ensureTabHost() { |
| 139 | if (mTabHost == null) { |
| 140 | this.setContentView(com.android.internal.R.layout.tab_content); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected void |
| 146 | onChildTitleChanged(Activity childActivity, CharSequence title) { |
| 147 | // Dorky implementation until we can have multiple activities running. |
| 148 | if (getLocalActivityManager().getCurrentActivity() == childActivity) { |
| 149 | View tabView = mTabHost.getCurrentTabView(); |
| 150 | if (tabView != null && tabView instanceof TextView) { |
| 151 | ((TextView) tabView).setText(title); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the {@link TabHost} the activity is using to host its tabs. |
| 158 | * |
| 159 | * @return the {@link TabHost} the activity is using to host its tabs. |
| 160 | */ |
| 161 | public TabHost getTabHost() { |
| 162 | ensureTabHost(); |
| 163 | return mTabHost; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the {@link TabWidget} the activity is using to draw the actual tabs. |
| 168 | * |
| 169 | * @return the {@link TabWidget} the activity is using to draw the actual tabs. |
| 170 | */ |
| 171 | public TabWidget getTabWidget() { |
| 172 | return mTabHost.getTabWidget(); |
| 173 | } |
| 174 | } |