2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.apis.support.app;
19 import com.example.android.apis.R;
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.FragmentActivity;
23 import android.support.v4.app.FragmentManager;
24 import android.support.v4.app.FragmentTransaction;
25 import android.support.v4.view.MenuCompat;
27 import android.os.Bundle;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.CheckBox;
36 * Demonstrates how fragments can participate in the options menu.
38 public class FragmentMenuSupport extends FragmentActivity {
44 // Update fragment visibility when check boxes are changed.
45 final OnClickListener mClickListener = new OnClickListener() {
46 public void onClick(View v) {
47 updateFragmentVisibility();
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.fragment_menu);
56 // Make sure the two menu fragments are created.
57 FragmentManager fm = getSupportFragmentManager();
58 FragmentTransaction ft = fm.beginTransaction();
59 mFragment1 = fm.findFragmentByTag("f1");
60 if (mFragment1 == null) {
61 mFragment1 = new MenuFragment();
62 ft.add(mFragment1, "f1");
64 mFragment2 = fm.findFragmentByTag("f2");
65 if (mFragment2 == null) {
66 mFragment2 = new Menu2Fragment();
67 ft.add(mFragment2, "f2");
71 // Watch check box clicks.
72 mCheckBox1 = (CheckBox)findViewById(R.id.menu1);
73 mCheckBox1.setOnClickListener(mClickListener);
74 mCheckBox2 = (CheckBox)findViewById(R.id.menu2);
75 mCheckBox2.setOnClickListener(mClickListener);
77 // Make sure fragments start out with correct visibility.
78 updateFragmentVisibility();
82 protected void onRestoreInstanceState(Bundle savedInstanceState) {
83 super.onRestoreInstanceState(savedInstanceState);
84 // Make sure fragments are updated after check box view state is restored.
85 updateFragmentVisibility();
88 // Update fragment visibility based on current check box state.
89 void updateFragmentVisibility() {
90 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
91 if (mCheckBox1.isChecked()) ft.show(mFragment1);
92 else ft.hide(mFragment1);
93 if (mCheckBox2.isChecked()) ft.show(mFragment2);
94 else ft.hide(mFragment2);
99 * A fragment that displays a menu. This fragment happens to not
100 * have a UI (it does not implement onCreateView), but it could also
101 * have one if it wanted.
103 public static class MenuFragment extends Fragment {
106 public void onCreate(Bundle savedInstanceState) {
107 super.onCreate(savedInstanceState);
108 setHasOptionsMenu(true);
112 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
114 item = menu.add("Menu 1a");
115 MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
116 item = menu.add("Menu 1b");
117 MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
122 * Second fragment with a menu.
124 public static class Menu2Fragment extends Fragment {
127 public void onCreate(Bundle savedInstanceState) {
128 super.onCreate(savedInstanceState);
129 setHasOptionsMenu(true);
133 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
135 item = menu.add("Menu 2");
136 MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);