1 /* 2 * Copyright (C) 2011 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 package com.example.android.apis.app; 17 18 import com.example.android.apis.R; 19 20 //BEGIN_INCLUDE(complete) 21 import android.app.ActionBar; 22 import android.app.ActionBar.Tab; 23 import android.app.Activity; 24 import android.app.Fragment; 25 import android.app.FragmentTransaction; 26 import android.os.Bundle; 27 import android.widget.Toast; 28 29 /** 30 * This demonstrates the use of action bar tabs and how they interact 31 * with other action bar features. 32 */ 33 public class FragmentTabs extends Activity { 34 @Override onCreate(Bundle savedInstanceState)35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 final ActionBar bar = getActionBar(); 39 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 40 bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 41 42 bar.addTab(bar.newTab() 43 .setText("Simple") 44 .setTabListener(new TabListener<FragmentStack.CountingFragment>( 45 this, "simple", FragmentStack.CountingFragment.class))); 46 bar.addTab(bar.newTab() 47 .setText("Contacts") 48 .setTabListener(new TabListener<LoaderCursor.CursorLoaderListFragment>( 49 this, "contacts", LoaderCursor.CursorLoaderListFragment.class))); 50 bar.addTab(bar.newTab() 51 .setText("Apps") 52 .setTabListener(new TabListener<LoaderCustom.AppListFragment>( 53 this, "apps", LoaderCustom.AppListFragment.class))); 54 bar.addTab(bar.newTab() 55 .setText("Throttle") 56 .setTabListener(new TabListener<LoaderThrottle.ThrottledLoaderListFragment>( 57 this, "throttle", LoaderThrottle.ThrottledLoaderListFragment.class))); 58 59 if (savedInstanceState != null) { 60 bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0)); 61 } 62 } 63 64 @Override onSaveInstanceState(Bundle outState)65 protected void onSaveInstanceState(Bundle outState) { 66 super.onSaveInstanceState(outState); 67 outState.putInt("tab", getActionBar().getSelectedNavigationIndex()); 68 } 69 70 public static class TabListener<T extends Fragment> implements ActionBar.TabListener { 71 private final Activity mActivity; 72 private final String mTag; 73 private final Class<T> mClass; 74 private final Bundle mArgs; 75 private Fragment mFragment; 76 TabListener(Activity activity, String tag, Class<T> clz)77 public TabListener(Activity activity, String tag, Class<T> clz) { 78 this(activity, tag, clz, null); 79 } 80 TabListener(Activity activity, String tag, Class<T> clz, Bundle args)81 public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) { 82 mActivity = activity; 83 mTag = tag; 84 mClass = clz; 85 mArgs = args; 86 87 // Check to see if we already have a fragment for this tab, probably 88 // from a previously saved state. If so, deactivate it, because our 89 // initial state is that a tab isn't shown. 90 mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag); 91 if (mFragment != null && !mFragment.isDetached()) { 92 FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction(); 93 ft.detach(mFragment); 94 ft.commit(); 95 } 96 } 97 onTabSelected(Tab tab, FragmentTransaction ft)98 public void onTabSelected(Tab tab, FragmentTransaction ft) { 99 if (mFragment == null) { 100 mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs); 101 ft.add(android.R.id.content, mFragment, mTag); 102 } else { 103 ft.attach(mFragment); 104 } 105 } 106 onTabUnselected(Tab tab, FragmentTransaction ft)107 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 108 if (mFragment != null) { 109 ft.detach(mFragment); 110 } 111 } 112 onTabReselected(Tab tab, FragmentTransaction ft)113 public void onTabReselected(Tab tab, FragmentTransaction ft) { 114 Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show(); 115 } 116 } 117 } 118 //END_INCLUDE(complete) 119