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 com.example.android.supportv4.R;
     20 
     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;
     25 
     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;
     33 
     34 /**
     35  * Demonstration of hiding and showing fragments.
     36  */
     37 public class FragmentHideShowSupport extends FragmentActivity {
     38 
     39     @Override
     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 
     57     void addShowHideListener(int buttonId, final Fragment fragment) {
     58         final Button button = (Button)findViewById(buttonId);
     59         button.setOnClickListener(new OnClickListener() {
     60             public void onClick(View v) {
     61                 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
     62                 ft.setCustomAnimations(android.R.anim.fade_in,
     63                         android.R.anim.fade_out);
     64                 if (fragment.isHidden()) {
     65                     ft.show(fragment);
     66                     button.setText("Hide");
     67                 } else {
     68                     ft.hide(fragment);
     69                     button.setText("Show");
     70                 }
     71                 ft.commit();
     72             }
     73         });
     74     }
     75 
     76     public static class FirstFragment extends Fragment {
     77         TextView mTextView;
     78 
     79         @Override
     80         public View onCreateView(LayoutInflater inflater, ViewGroup container,
     81                 Bundle savedInstanceState) {
     82             View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
     83             View tv = v.findViewById(R.id.msg);
     84             ((TextView)tv).setText("The fragment saves and restores this text.");
     85 
     86             // Retrieve the text editor, and restore the last saved state if needed.
     87             mTextView = (TextView)v.findViewById(R.id.saved);
     88             if (savedInstanceState != null) {
     89                 mTextView.setText(savedInstanceState.getCharSequence("text"));
     90             }
     91             return v;
     92         }
     93 
     94         @Override
     95         public void onSaveInstanceState(Bundle outState) {
     96             super.onSaveInstanceState(outState);
     97 
     98             // Remember the current text, to restore if we later restart.
     99             outState.putCharSequence("text", mTextView.getText());
    100         }
    101     }
    102 
    103     public static class SecondFragment extends Fragment {
    104 
    105         @Override
    106         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    107                 Bundle savedInstanceState) {
    108             View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
    109             View tv = v.findViewById(R.id.msg);
    110             ((TextView)tv).setText("The TextView saves and restores this text.");
    111 
    112             // Retrieve the text editor and tell it to save and restore its state.
    113             // Note that you will often set this in the layout XML, but since
    114             // we are sharing our layout with the other fragment we will customize
    115             // it here.
    116             ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);
    117             return v;
    118         }
    119     }
    120 }
    121