1 /* 2 * Copyright (C) 2012 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 17 package com.example.android.apis.app; 18 19 import com.example.android.apis.R; 20 21 import android.app.Fragment; 22 import android.app.FragmentManager; 23 import android.app.FragmentTransaction; 24 import android.os.Bundle; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.view.ViewGroup; 29 import android.widget.CheckBox; 30 31 /** 32 * Demonstrates how fragments can participate in the options menu. 33 */ 34 public class FragmentMenuFragment extends Fragment { 35 Fragment mFragment1; 36 Fragment mFragment2; 37 CheckBox mCheckBox1; 38 CheckBox mCheckBox2; 39 40 // Update fragment visibility when check boxes are changed. 41 final OnClickListener mClickListener = new OnClickListener() { 42 public void onClick(View v) { 43 updateFragmentVisibility(); 44 } 45 }; 46 47 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)48 public View onCreateView(LayoutInflater inflater, ViewGroup container, 49 Bundle savedInstanceState) { 50 View v = inflater.inflate(R.layout.fragment_menu, container, false); 51 52 // Make sure the two menu fragments are created. 53 FragmentManager fm = getChildFragmentManager(); 54 FragmentTransaction ft = fm.beginTransaction(); 55 mFragment1 = fm.findFragmentByTag("f1"); 56 if (mFragment1 == null) { 57 mFragment1 = new FragmentMenu.MenuFragment(); 58 ft.add(mFragment1, "f1"); 59 } 60 mFragment2 = fm.findFragmentByTag("f2"); 61 if (mFragment2 == null) { 62 mFragment2 = new FragmentMenu.Menu2Fragment(); 63 ft.add(mFragment2, "f2"); 64 } 65 ft.commit(); 66 67 // Watch check box clicks. 68 mCheckBox1 = (CheckBox)v.findViewById(R.id.menu1); 69 mCheckBox1.setOnClickListener(mClickListener); 70 mCheckBox2 = (CheckBox)v.findViewById(R.id.menu2); 71 mCheckBox2.setOnClickListener(mClickListener); 72 73 // Make sure fragments start out with correct visibility. 74 updateFragmentVisibility(); 75 76 return v; 77 } 78 79 @Override onViewStateRestored(Bundle savedInstanceState)80 public void onViewStateRestored(Bundle savedInstanceState) { 81 super.onViewStateRestored(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. updateFragmentVisibility()87 void updateFragmentVisibility() { 88 FragmentTransaction ft = getChildFragmentManager().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