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