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.LayoutInflater; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 import android.widget.TextView; 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 * Demonstration of hiding and showing fragments. 36 */ 37 public class FragmentHideShowSupport extends FragmentActivity { 38 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.fragment_hide_show_support); 43 44 // The content view embeds two fragments; now retrieve them and attach 45 // their "hide" button. 46 FragmentManager fm = getSupportFragmentManager(); 47 Fragment fragment1 = fm.findFragmentById(R.id.fragment1); 48 addShowHideListener(R.id.frag1hide, fragment1); 49 final Button button1 = (Button)findViewById(R.id.frag1hide); 50 button1.setText(fragment1.isHidden() ? "Show" : "Hide"); 51 Fragment fragment2 = fm.findFragmentById(R.id.fragment2); 52 addShowHideListener(R.id.frag2hide, fragment2); 53 final Button button2 = (Button)findViewById(R.id.frag2hide); 54 button2.setText(fragment2.isHidden() ? "Show" : "Hide"); 55 } 56 addShowHideListener(int buttonId, final Fragment fragment)57 void addShowHideListener(int buttonId, final Fragment fragment) { 58 final Button button = (Button)findViewById(buttonId); 59 button.setOnClickListener(new OnClickListener() { 60 @Override 61 public void onClick(View v) { 62 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 63 ft.setCustomAnimations(android.R.anim.fade_in, 64 android.R.anim.fade_out); 65 if (fragment.isHidden()) { 66 ft.show(fragment); 67 button.setText("Hide"); 68 } else { 69 ft.hide(fragment); 70 button.setText("Show"); 71 } 72 ft.commit(); 73 } 74 }); 75 } 76 77 public static class FirstFragment extends Fragment { 78 TextView mTextView; 79 80 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)81 public View onCreateView(LayoutInflater inflater, ViewGroup container, 82 Bundle savedInstanceState) { 83 View v = inflater.inflate(R.layout.labeled_text_edit, container, false); 84 View tv = v.findViewById(R.id.msg); 85 ((TextView)tv).setText("The fragment saves and restores this text."); 86 87 // Retrieve the text editor, and restore the last saved state if needed. 88 mTextView = (TextView)v.findViewById(R.id.saved); 89 if (savedInstanceState != null) { 90 mTextView.setText(savedInstanceState.getCharSequence("text")); 91 } 92 return v; 93 } 94 95 @Override onSaveInstanceState(Bundle outState)96 public void onSaveInstanceState(Bundle outState) { 97 super.onSaveInstanceState(outState); 98 99 // Remember the current text, to restore if we later restart. 100 outState.putCharSequence("text", mTextView.getText()); 101 } 102 } 103 104 public static class SecondFragment extends Fragment { 105 106 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)107 public View onCreateView(LayoutInflater inflater, ViewGroup container, 108 Bundle savedInstanceState) { 109 View v = inflater.inflate(R.layout.labeled_text_edit, container, false); 110 View tv = v.findViewById(R.id.msg); 111 ((TextView)tv).setText("The TextView saves and restores this text."); 112 113 // Retrieve the text editor and tell it to save and restore its state. 114 // Note that you will often set this in the layout XML, but since 115 // we are sharing our layout with the other fragment we will customize 116 // it here. 117 ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true); 118 return v; 119 } 120 } 121 } 122