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
17package com.example.android.supportv7.app;
18
19import android.os.Bundle;
20import android.view.Menu;
21import android.view.MenuInflater;
22import android.view.MenuItem;
23import android.view.View;
24import android.view.View.OnClickListener;
25import android.widget.CheckBox;
26import android.widget.Toast;
27
28import androidx.appcompat.app.AppCompatActivity;
29import androidx.fragment.app.Fragment;
30import androidx.fragment.app.FragmentManager;
31import androidx.fragment.app.FragmentTransaction;
32
33import com.example.android.supportv7.R;
34
35/**
36 * Demonstrates how fragments can participate in the options menu.
37 */
38public 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
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
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.
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
132        public void onCreate(Bundle savedInstanceState) {
133            super.onCreate(savedInstanceState);
134            setHasOptionsMenu(true);
135        }
136
137        @Override
138        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
139            menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
140            menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
141            super.onCreateOptionsMenu(menu, inflater);
142        }
143
144        @Override
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
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
180        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
181            menu.add("Menu 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
182        }
183
184        @Override
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
200        public void onCreate(Bundle savedInstanceState) {
201            super.onCreate(savedInstanceState);
202            setHasOptionsMenu(true);
203        }
204
205        @Override
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
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
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
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