• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 com.example.android.supportv7.app;
18 
19 import com.example.android.supportv7.R;
20 
21 import android.os.Bundle;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentManager;
24 import android.support.v4.app.FragmentTransaction;
25 import android.support.v4.view.MenuItemCompat;
26 import android.support.v7.app.AppCompatActivity;
27 import android.view.Menu;
28 import android.view.MenuInflater;
29 import android.view.MenuItem;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.widget.CheckBox;
33 import android.widget.Toast;
34 
35 /**
36  * Demonstrates how fragments can participate in the options menu.
37  */
38 public class ActionBarFragmentMenu extends AppCompatActivity {
39     MenuFragment mFragment1;
40     Menu2Fragment mFragment2;
41     CheckBox mCheckBox1;
42     CheckBox mCheckBox2;
43     CheckBox mCheckBox3;
44     CheckBox mHasOptionsMenu;
45     CheckBox mMenuVisibility;
46 
47     // Update fragment visibility when check boxes are changed.
48     final OnClickListener mClickListener = new OnClickListener() {
49         @Override
50         public void onClick(View v) {
51             updateFragmentVisibility();
52         }
53     };
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentView(R.layout.action_bar_fragment_menu);
59 
60         // Make sure the two menu fragments are created.
61         FragmentManager fm = getSupportFragmentManager();
62         FragmentTransaction ft = fm.beginTransaction();
63         mFragment1 = (MenuFragment)fm.findFragmentByTag("f1");
64         if (mFragment1 == null) {
65             mFragment1 = new MenuFragment();
66             ft.add(mFragment1, "f1");
67         }
68         mFragment2 = (Menu2Fragment)fm.findFragmentByTag("f2");
69         if (mFragment2 == null) {
70             mFragment2 = new Menu2Fragment();
71             ft.add(mFragment2, "f2");
72         }
73         ft.commit();
74 
75         // Watch check box clicks.
76         mCheckBox1 = (CheckBox)findViewById(R.id.menu1);
77         mCheckBox1.setOnClickListener(mClickListener);
78         mCheckBox2 = (CheckBox)findViewById(R.id.menu2);
79         mCheckBox2.setOnClickListener(mClickListener);
80         mCheckBox3 = (CheckBox)findViewById(R.id.menu3);
81         mCheckBox3.setOnClickListener(mClickListener);
82         mHasOptionsMenu = (CheckBox)findViewById(R.id.has_options_menu);
83         mHasOptionsMenu.setOnClickListener(mClickListener);
84         mMenuVisibility = (CheckBox)findViewById(R.id.menu_visibility);
85         mMenuVisibility.setOnClickListener(mClickListener);
86 
87         // Make sure fragments start out with correct visibility.
88         updateFragmentVisibility();
89     }
90 
91     @Override
onRestoreInstanceState(Bundle savedInstanceState)92     protected void onRestoreInstanceState(Bundle savedInstanceState) {
93         super.onRestoreInstanceState(savedInstanceState);
94         // Make sure fragments are updated after check box view state is restored.
95         updateFragmentVisibility();
96     }
97 
98     // Update fragment visibility based on current check box state.
updateFragmentVisibility()99     void updateFragmentVisibility() {
100         // Update top level fragments.
101         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
102         if (mCheckBox1.isChecked()) ft.show(mFragment1);
103         else ft.hide(mFragment1);
104         if (mCheckBox2.isChecked()) ft.show(mFragment2);
105         else ft.hide(mFragment2);
106         ft.commit();
107 
108         mFragment1.setHasOptionsMenu(mHasOptionsMenu.isChecked());
109         mFragment1.setMenuVisibility(mMenuVisibility.isChecked());
110         mFragment2.setHasOptionsMenu(mHasOptionsMenu.isChecked());
111         mFragment2.setMenuVisibility(mMenuVisibility.isChecked());
112 
113         // Update the nested fragment.
114         if (mFragment2.mFragment3 != null) {
115             ft = mFragment2.getFragmentManager().beginTransaction();
116             if (mCheckBox3.isChecked()) ft.show(mFragment2.mFragment3);
117             else ft.hide(mFragment2.mFragment3);
118             ft.commit();
119 
120             mFragment2.mFragment3.setHasOptionsMenu(mHasOptionsMenu.isChecked());
121             mFragment2.mFragment3.setMenuVisibility(mMenuVisibility.isChecked());
122         }
123     }
124 
125     /**
126      * A fragment that displays a menu.  This fragment happens to not
127      * have a UI (it does not implement onCreateView), but it could also
128      * have one if it wanted.
129      */
130     public static class MenuFragment extends Fragment {
131         @Override
onCreate(Bundle savedInstanceState)132         public void onCreate(Bundle savedInstanceState) {
133             super.onCreate(savedInstanceState);
134             setHasOptionsMenu(true);
135         }
136 
137         @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)138         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
139             MenuItemCompat.setShowAsAction(menu.add("Menu 1a"), MenuItem.SHOW_AS_ACTION_IF_ROOM);
140             MenuItemCompat.setShowAsAction(menu.add("Menu 1b"), MenuItem.SHOW_AS_ACTION_IF_ROOM);
141             super.onCreateOptionsMenu(menu, inflater);
142         }
143 
144         @Override
onOptionsItemSelected(MenuItem item)145         public boolean onOptionsItemSelected(MenuItem item) {
146             if (item.getTitle().equals("Menu 1a")) {
147                 Toast.makeText(getActivity(), "Selected Menu 1a.", Toast.LENGTH_SHORT).show();
148                 return true;
149             }
150             if (item.getTitle().equals("Menu 1b")) {
151                 Toast.makeText(getActivity(), "Selected Menu 1b.", Toast.LENGTH_SHORT).show();
152                 return true;
153             }
154             return super.onOptionsItemSelected(item);
155         }
156     }
157 
158     /**
159      * Second fragment with a menu.
160      */
161     public static class Menu2Fragment extends Fragment {
162         Menu3Fragment mFragment3;
163 
164         @Override
onCreate(Bundle savedInstanceState)165         public void onCreate(Bundle savedInstanceState) {
166             super.onCreate(savedInstanceState);
167             setHasOptionsMenu(true);
168 
169             FragmentManager fm = getChildFragmentManager();
170             FragmentTransaction ft = fm.beginTransaction();
171             mFragment3 = (Menu3Fragment)fm.findFragmentByTag("f3");
172             if (mFragment3 == null) {
173                 mFragment3 = new Menu3Fragment();
174                 ft.add(mFragment3, "f3");
175             }
176             ft.commit();
177         }
178 
179         @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)180         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
181             MenuItemCompat.setShowAsAction(menu.add("Menu 2"), MenuItem.SHOW_AS_ACTION_IF_ROOM);
182         }
183 
184         @Override
onOptionsItemSelected(MenuItem item)185         public boolean onOptionsItemSelected(MenuItem item) {
186             if (item.getTitle().equals("Menu 2")) {
187                 Toast.makeText(getActivity(), "Selected Menu 2.", Toast.LENGTH_SHORT).show();
188                 return true;
189             }
190             return false;
191         }
192     }
193 
194     /**
195      * Third fragment with a menu.
196      * This one is nested within the second.
197      */
198     public static class Menu3Fragment extends Fragment {
199         @Override
onCreate(Bundle savedInstanceState)200         public void onCreate(Bundle savedInstanceState) {
201             super.onCreate(savedInstanceState);
202             setHasOptionsMenu(true);
203         }
204 
205         @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)206         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
207             Toast.makeText(getActivity(), "Created nested fragment's menu.",
208                     Toast.LENGTH_SHORT).show();
209             inflater.inflate(R.menu.display_options_actions, menu);
210             super.onCreateOptionsMenu(menu, inflater);
211         }
212 
213         @Override
onDestroyOptionsMenu()214         public void onDestroyOptionsMenu() {
215             Toast.makeText(getActivity(), "Destroyed nested fragment's menu.",
216                     Toast.LENGTH_SHORT).show();
217             super.onDestroyOptionsMenu();
218         }
219 
220         @Override
onPrepareOptionsMenu(Menu menu)221         public void onPrepareOptionsMenu(Menu menu) {
222             Toast.makeText(getActivity(), "Prepared nested fragment's menu.",
223                     Toast.LENGTH_SHORT).show();
224             super.onPrepareOptionsMenu(menu);
225         }
226 
227         @Override
onOptionsItemSelected(MenuItem item)228         public boolean onOptionsItemSelected(MenuItem item) {
229             if (item.getItemId() == R.id.simple_item) {
230                 Toast.makeText(getActivity(), "Selected nested fragment's menu item.",
231                         Toast.LENGTH_SHORT).show();
232                 return true;
233             }
234             return super.onOptionsItemSelected(item);
235         }
236     }
237 }
238