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