2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.supportv4.app;
19 import com.example.android.supportv4.R;
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.FragmentActivity;
23 import android.support.v4.app.FragmentManager;
24 import android.support.v4.app.FragmentTransaction;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.View.OnClickListener;
31 import android.widget.Button;
32 import android.widget.TextView;
35 * Demonstration of hiding and showing fragments.
37 public class FragmentHideShowSupport extends FragmentActivity {
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.fragment_hide_show_support);
44 // The content view embeds two fragments; now retrieve them and attach
45 // their "hide" button.
46 FragmentManager fm = getSupportFragmentManager();
47 addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
48 addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
51 void addShowHideListener(int buttonId, final Fragment fragment) {
52 final Button button = (Button)findViewById(buttonId);
53 button.setOnClickListener(new OnClickListener() {
54 public void onClick(View v) {
55 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
56 ft.setCustomAnimations(android.R.anim.fade_in,
57 android.R.anim.fade_out);
58 if (fragment.isHidden()) {
60 button.setText("Hide");
63 button.setText("Show");
70 public static class FirstFragment extends Fragment {
74 public View onCreateView(LayoutInflater inflater, ViewGroup container,
75 Bundle savedInstanceState) {
76 View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
77 View tv = v.findViewById(R.id.msg);
78 ((TextView)tv).setText("The fragment saves and restores this text.");
80 // Retrieve the text editor, and restore the last saved state if needed.
81 mTextView = (TextView)v.findViewById(R.id.saved);
82 if (savedInstanceState != null) {
83 mTextView.setText(savedInstanceState.getCharSequence("text"));
89 public void onSaveInstanceState(Bundle outState) {
90 super.onSaveInstanceState(outState);
92 // Remember the current text, to restore if we later restart.
93 outState.putCharSequence("text", mTextView.getText());
97 public static class SecondFragment extends Fragment {
100 public View onCreateView(LayoutInflater inflater, ViewGroup container,
101 Bundle savedInstanceState) {
102 View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
103 View tv = v.findViewById(R.id.msg);
104 ((TextView)tv).setText("The TextView saves and restores this text.");
106 // Retrieve the text editor and tell it to save and restore its state.
107 // Note that you will often set this in the layout XML, but since
108 // we are sharing our layout with the other fragment we will customize
110 ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);