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