FragmentStackFragmentSupport.java revision e2104f4b5c8e3ad63570306a25e61502dfe4c418
1/* 2 * Copyright (C) 2012 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.supportv4.app; 18 19import android.os.Bundle; 20import android.support.v4.app.Fragment; 21import android.support.v4.app.FragmentManager; 22import android.support.v4.app.FragmentTransaction; 23import android.view.LayoutInflater; 24import android.view.View; 25import android.view.ViewGroup; 26import android.view.View.OnClickListener; 27import android.widget.Button; 28 29import com.example.android.supportv4.R; 30 31public class FragmentStackFragmentSupport extends Fragment { 32 int mStackLevel = 1; 33 34 @Override 35 public void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 38 if (savedInstanceState == null) { 39 // Do first time initialization -- add initial fragment. 40 Fragment newFragment = FragmentStackSupport.CountingFragment.newInstance(mStackLevel); 41 FragmentTransaction ft = getChildFragmentManager().beginTransaction(); 42 ft.add(R.id.simple_fragment, newFragment).commit(); 43 } else { 44 mStackLevel = savedInstanceState.getInt("level"); 45 } 46 } 47 48 @Override 49 public View onCreateView(LayoutInflater inflater, ViewGroup container, 50 Bundle savedInstanceState) { 51 View v = inflater.inflate(R.layout.fragment_stack, container, false); 52 53 // Watch for button clicks. 54 Button button = (Button)v.findViewById(R.id.new_fragment); 55 button.setOnClickListener(new OnClickListener() { 56 @Override 57 public void onClick(View v) { 58 addFragmentToStack(); 59 } 60 }); 61 button = (Button)v.findViewById(R.id.delete_fragment); 62 button.setOnClickListener(new OnClickListener() { 63 @Override 64 public void onClick(View v) { 65 getChildFragmentManager().popBackStack(); 66 } 67 }); 68 button = (Button)v.findViewById(R.id.home); 69 button.setOnClickListener(new OnClickListener() { 70 @Override 71 public void onClick(View v) { 72 // If there is a back stack, pop it all. 73 FragmentManager fm = getChildFragmentManager(); 74 if (fm.getBackStackEntryCount() > 0) { 75 fm.popBackStack(fm.getBackStackEntryAt(0).getId(), 76 FragmentManager.POP_BACK_STACK_INCLUSIVE); 77 } 78 } 79 }); 80 81 return v; 82 } 83 84 @Override 85 public void onSaveInstanceState(Bundle outState) { 86 super.onSaveInstanceState(outState); 87 outState.putInt("level", mStackLevel); 88 } 89 90 void addFragmentToStack() { 91 mStackLevel++; 92 93 // Instantiate a new fragment. 94 Fragment newFragment = FragmentStackSupport.CountingFragment.newInstance(mStackLevel); 95 96 // Add the fragment to the activity, pushing this transaction 97 // on to the back stack. 98 FragmentTransaction ft = getChildFragmentManager().beginTransaction(); 99 ft.replace(R.id.simple_fragment, newFragment); 100 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 101 ft.addToBackStack(null); 102 ft.commit(); 103 } 104} 105