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