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