1/*
2 * Copyright (C) 2010 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.apis.app;
18
19import com.example.android.apis.R;
20
21import android.app.Activity;
22import android.app.Fragment;
23import android.app.FragmentManager;
24import android.app.FragmentTransaction;
25import android.os.Bundle;
26import android.view.Menu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.View.OnClickListener;
31import android.widget.CheckBox;
32
33/**
34 * Demonstrates how fragments can participate in the options menu.
35 */
36public class FragmentMenu extends Activity {
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 = getFragmentManager();
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 = getFragmentManager().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            menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
112            menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
113        }
114    }
115
116    /**
117     * Second fragment with a menu.
118     */
119    public static class Menu2Fragment extends Fragment {
120
121        @Override
122        public void onCreate(Bundle savedInstanceState) {
123            super.onCreate(savedInstanceState);
124            setHasOptionsMenu(true);
125        }
126
127        @Override
128        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
129            menu.add("Menu 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
130        }
131    }
132}
133