1 /*
2  * Copyright (C) 2012 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 //BEGIN_INCLUDE(complete)
19 import android.app.ActionBar;
20 import android.app.ActionBar.Tab;
21 import android.app.Activity;
22 import android.app.Fragment;
23 import android.app.FragmentManager;
24 import android.app.FragmentTransaction;
25 import android.os.Bundle;
26 import android.widget.Toast;
27 
28 /**
29  * This demonstrates the use of action bar tabs and how they interact
30  * with other action bar features.
31  */
32 public class FragmentNestingTabs extends Activity {
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         FragmentManager.enableDebugLogging(true);
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("Menus")
44                 .setTabListener(new TabListener<FragmentMenuFragment>(
45                         this, "menus", FragmentMenuFragment.class)));
46         bar.addTab(bar.newTab()
47                 .setText("Args")
48                 .setTabListener(new TabListener<FragmentArgumentsFragment>(
49                         this, "args", FragmentArgumentsFragment.class)));
50         bar.addTab(bar.newTab()
51                 .setText("Stack")
52                 .setTabListener(new TabListener<FragmentStackFragment>(
53                         this, "stack", FragmentStackFragment.class)));
54         bar.addTab(bar.newTab()
55                 .setText("Tabs")
56                 .setTabListener(new TabListener<FragmentTabsFragment>(
57                         this, "tabs", FragmentTabsFragment.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