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