FragmentMenuSupport.java revision 458543f38db49fdcb1764f007ded33820964c3fe
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
17package com.example.android.supportv4.app;
18
19import android.os.Bundle;
20import android.support.v4.app.Fragment;
21import android.support.v4.app.FragmentActivity;
22import android.support.v4.app.FragmentManager;
23import android.support.v4.app.FragmentTransaction;
24import android.view.Menu;
25import android.view.MenuInflater;
26import android.view.MenuItem;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.CheckBox;
30
31import com.example.android.supportv4.R;
32
33/**
34 * Demonstrates how fragments can participate in the options menu.
35 */
36public class FragmentMenuSupport extends FragmentActivity {
37    Fragment mFragment1;
38    Fragment mFragment2;
39    CheckBox mCheckBox1;
40    CheckBox mCheckBox2;
41
42    // Update fragment visibility when check boxes are changed.
43    final OnClickListener mClickListener = new OnClickListener() {
44        public void onClick(View v) {
45            updateFragmentVisibility();
46        }
47    };
48
49    @Override
50    protected void onCreate(Bundle savedInstanceState) {
51        super.onCreate(savedInstanceState);
52        setContentView(R.layout.fragment_menu);
53
54        // Make sure the two menu fragments are created.
55        FragmentManager fm = getSupportFragmentManager();
56        FragmentTransaction ft = fm.beginTransaction();
57        mFragment1 = fm.findFragmentByTag("f1");
58        if (mFragment1 == null) {
59            mFragment1 = new MenuFragment();
60            ft.add(mFragment1, "f1");
61        }
62        mFragment2 = fm.findFragmentByTag("f2");
63        if (mFragment2 == null) {
64            mFragment2 = new Menu2Fragment();
65            ft.add(mFragment2, "f2");
66        }
67        ft.commit();
68
69        // Watch check box clicks.
70        mCheckBox1 = (CheckBox)findViewById(R.id.menu1);
71        mCheckBox1.setOnClickListener(mClickListener);
72        mCheckBox2 = (CheckBox)findViewById(R.id.menu2);
73        mCheckBox2.setOnClickListener(mClickListener);
74
75        // Make sure fragments start out with correct visibility.
76        updateFragmentVisibility();
77    }
78
79    @Override
80    protected void onRestoreInstanceState(Bundle savedInstanceState) {
81        super.onRestoreInstanceState(savedInstanceState);
82        // Make sure fragments are updated after check box view state is restored.
83        updateFragmentVisibility();
84    }
85
86    // Update fragment visibility based on current check box state.
87    void updateFragmentVisibility() {
88        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
89        if (mCheckBox1.isChecked()) ft.show(mFragment1);
90        else ft.hide(mFragment1);
91        if (mCheckBox2.isChecked()) ft.show(mFragment2);
92        else ft.hide(mFragment2);
93        ft.commit();
94    }
95
96    /**
97     * A fragment that displays a menu.  This fragment happens to not
98     * have a UI (it does not implement onCreateView), but it could also
99     * have one if it wanted.
100     */
101    public static class MenuFragment extends Fragment {
102
103        @Override
104        public void onCreate(Bundle savedInstanceState) {
105            super.onCreate(savedInstanceState);
106            setHasOptionsMenu(true);
107        }
108
109        @Override
110        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
111            MenuItem item;
112            item = menu.add("Menu 1a");
113            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
114            item = menu.add("Menu 1b");
115            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
116        }
117    }
118
119    /**
120     * Second fragment with a menu.
121     */
122    public static class Menu2Fragment extends Fragment {
123
124        @Override
125        public void onCreate(Bundle savedInstanceState) {
126            super.onCreate(savedInstanceState);
127            setHasOptionsMenu(true);
128        }
129
130        @Override
131        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
132            MenuItem item;
133            item = menu.add("Menu 2");
134            item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
135        }
136    }
137}
138