• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
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  * @deprecated New applications should use Fragments instead of this class;
32  * to continue to run on older devices, you can use the v4 support library
33  * which provides a version of the Fragment API that is compatible down to
34  * {@link android.os.Build.VERSION_CODES#DONUT}.
35  */
36 @Deprecated
37 public class TabActivity extends ActivityGroup {
38     private TabHost mTabHost;
39     private String mDefaultTab = null;
40     private int mDefaultTabIndex = -1;
41 
TabActivity()42     public TabActivity() {
43     }
44 
45     /**
46      * Sets the default tab that is the first tab highlighted.
47      *
48      * @param tag the name of the default tab
49      */
setDefaultTab(String tag)50     public void setDefaultTab(String tag) {
51         mDefaultTab = tag;
52         mDefaultTabIndex = -1;
53     }
54 
55     /**
56      * Sets the default tab that is the first tab highlighted.
57      *
58      * @param index the index of the default tab
59      */
setDefaultTab(int index)60     public void setDefaultTab(int index) {
61         mDefaultTab = null;
62         mDefaultTabIndex = index;
63     }
64 
65     @Override
onRestoreInstanceState(Bundle state)66     protected void onRestoreInstanceState(Bundle state) {
67         super.onRestoreInstanceState(state);
68         ensureTabHost();
69         String cur = state.getString("currentTab");
70         if (cur != null) {
71             mTabHost.setCurrentTabByTag(cur);
72         }
73         if (mTabHost.getCurrentTab() < 0) {
74             if (mDefaultTab != null) {
75                 mTabHost.setCurrentTabByTag(mDefaultTab);
76             } else if (mDefaultTabIndex >= 0) {
77                 mTabHost.setCurrentTab(mDefaultTabIndex);
78             }
79         }
80     }
81 
82     @Override
onPostCreate(Bundle icicle)83     protected void onPostCreate(Bundle icicle) {
84         super.onPostCreate(icicle);
85 
86         ensureTabHost();
87 
88         if (mTabHost.getCurrentTab() == -1) {
89             mTabHost.setCurrentTab(0);
90         }
91     }
92 
93     @Override
onSaveInstanceState(Bundle outState)94     protected void onSaveInstanceState(Bundle outState) {
95         super.onSaveInstanceState(outState);
96         String currentTabTag = mTabHost.getCurrentTabTag();
97         if (currentTabTag != null) {
98             outState.putString("currentTab", currentTabTag);
99         }
100     }
101 
102     /**
103      * Updates the screen state (current list and other views) when the
104      * content changes.
105      *
106      *@see Activity#onContentChanged()
107      */
108     @Override
onContentChanged()109     public void onContentChanged() {
110         super.onContentChanged();
111         mTabHost = findViewById(com.android.internal.R.id.tabhost);
112 
113         if (mTabHost == null) {
114             throw new RuntimeException(
115                     "Your content must have a TabHost whose id attribute is " +
116                     "'android.R.id.tabhost'");
117         }
118         mTabHost.setup(getLocalActivityManager());
119     }
120 
ensureTabHost()121     private void ensureTabHost() {
122         if (mTabHost == null) {
123             this.setContentView(com.android.internal.R.layout.tab_content);
124         }
125     }
126 
127     @Override
128     protected void
onChildTitleChanged(Activity childActivity, CharSequence title)129     onChildTitleChanged(Activity childActivity, CharSequence title) {
130         // Dorky implementation until we can have multiple activities running.
131         if (getLocalActivityManager().getCurrentActivity() == childActivity) {
132             View tabView = mTabHost.getCurrentTabView();
133             if (tabView != null && tabView instanceof TextView) {
134                 ((TextView) tabView).setText(title);
135             }
136         }
137     }
138 
139     /**
140      * Returns the {@link TabHost} the activity is using to host its tabs.
141      *
142      * @return the {@link TabHost} the activity is using to host its tabs.
143      */
getTabHost()144     public TabHost getTabHost() {
145         ensureTabHost();
146         return mTabHost;
147     }
148 
149     /**
150      * Returns the {@link TabWidget} the activity is using to draw the actual tabs.
151      *
152      * @return the {@link TabWidget} the activity is using to draw the actual tabs.
153      */
getTabWidget()154     public TabWidget getTabWidget() {
155         return mTabHost.getTabWidget();
156     }
157 }
158