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