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 17 package com.example.android.supportv4.app; 18 19 import android.os.Bundle; 20 import android.support.v4.app.Fragment; 21 import android.support.v4.app.FragmentManager; 22 import android.support.v4.app.FragmentTransaction; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.view.View.OnClickListener; 27 import android.widget.Button; 28 29 import com.example.android.supportv4.R; 30 31 public 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 public void onClick(View v) { 57 addFragmentToStack(); 58 } 59 }); 60 button = (Button)v.findViewById(R.id.delete_fragment); 61 button.setOnClickListener(new OnClickListener() { 62 public void onClick(View v) { 63 getChildFragmentManager().popBackStack(); 64 } 65 }); 66 button = (Button)v.findViewById(R.id.home); 67 button.setOnClickListener(new OnClickListener() { 68 public void onClick(View v) { 69 // If there is a back stack, pop it all. 70 FragmentManager fm = getChildFragmentManager(); 71 if (fm.getBackStackEntryCount() > 0) { 72 fm.popBackStack(fm.getBackStackEntryAt(0).getId(), 73 FragmentManager.POP_BACK_STACK_INCLUSIVE); 74 } 75 } 76 }); 77 78 return v; 79 } 80 81 @Override 82 public void onSaveInstanceState(Bundle outState) { 83 super.onSaveInstanceState(outState); 84 outState.putInt("level", mStackLevel); 85 } 86 87 void addFragmentToStack() { 88 mStackLevel++; 89 90 // Instantiate a new fragment. 91 Fragment newFragment = FragmentStackSupport.CountingFragment.newInstance(mStackLevel); 92 93 // Add the fragment to the activity, pushing this transaction 94 // on to the back stack. 95 FragmentTransaction ft = getChildFragmentManager().beginTransaction(); 96 ft.replace(R.id.simple_fragment, newFragment); 97 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 98 ft.addToBackStack(null); 99 ft.commit(); 100 } 101 } 102