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 android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 import android.widget.TextView; 26 27 import androidx.core.content.ContextCompat; 28 import androidx.core.view.ViewCompat; 29 import androidx.fragment.app.Fragment; 30 import androidx.fragment.app.FragmentActivity; 31 import androidx.fragment.app.FragmentManager; 32 import androidx.fragment.app.FragmentTransaction; 33 34 import com.example.android.supportv4.R; 35 36 public class FragmentStackSupport extends FragmentActivity { 37 int mStackLevel = 1; 38 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.fragment_stack); 43 44 // Watch for button clicks. 45 Button button = findViewById(R.id.new_fragment); 46 button.setOnClickListener(new OnClickListener() { 47 @Override 48 public void onClick(View v) { 49 addFragmentToStack(); 50 } 51 }); 52 button = findViewById(R.id.home); 53 button.setOnClickListener(new OnClickListener() { 54 @Override 55 public void onClick(View v) { 56 // If there is a back stack, pop it all. 57 FragmentManager fm = getSupportFragmentManager(); 58 if (fm.getBackStackEntryCount() > 0) { 59 fm.popBackStack(fm.getBackStackEntryAt(0).getId(), 60 FragmentManager.POP_BACK_STACK_INCLUSIVE); 61 } 62 } 63 }); 64 65 if (savedInstanceState == null) { 66 // Do first time initialization -- add initial fragment. 67 Fragment newFragment = CountingFragment.newInstance(mStackLevel); 68 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 69 ft.add(R.id.simple_fragment, newFragment).commit(); 70 } else { 71 mStackLevel = savedInstanceState.getInt("level"); 72 } 73 } 74 75 @Override onSaveInstanceState(Bundle outState)76 public void onSaveInstanceState(Bundle outState) { 77 super.onSaveInstanceState(outState); 78 outState.putInt("level", mStackLevel); 79 } 80 81 //BEGIN_INCLUDE(add_stack) addFragmentToStack()82 void addFragmentToStack() { 83 mStackLevel++; 84 85 // Instantiate a new fragment. 86 Fragment newFragment = CountingFragment.newInstance(mStackLevel); 87 88 // Add the fragment to the activity, pushing this transaction 89 // on to the back stack. 90 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 91 ft.replace(R.id.simple_fragment, newFragment); 92 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 93 ft.addToBackStack(null); 94 ft.commit(); 95 } 96 //END_INCLUDE(add_stack) 97 98 //BEGIN_INCLUDE(fragment) 99 public static class CountingFragment extends Fragment { 100 int mNum; 101 102 /** 103 * Create a new instance of CountingFragment, providing "num" 104 * as an argument. 105 */ newInstance(int num)106 static CountingFragment newInstance(int num) { 107 CountingFragment f = new CountingFragment(); 108 109 // Supply num input as an argument. 110 Bundle args = new Bundle(); 111 args.putInt("num", num); 112 f.setArguments(args); 113 114 return f; 115 } 116 117 /** 118 * When creating, retrieve this instance's number from its arguments. 119 */ 120 @Override onCreate(Bundle savedInstanceState)121 public void onCreate(Bundle savedInstanceState) { 122 super.onCreate(savedInstanceState); 123 mNum = getArguments() != null ? getArguments().getInt("num") : 1; 124 } 125 126 /** 127 * The Fragment's UI is just a simple text view showing its 128 * instance number. 129 */ 130 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)131 public View onCreateView(LayoutInflater inflater, ViewGroup container, 132 Bundle savedInstanceState) { 133 View v = inflater.inflate(R.layout.hello_world, container, false); 134 View tv = v.findViewById(R.id.text); 135 ((TextView) tv).setText("Fragment #" + mNum); 136 ViewCompat.setBackground(tv, 137 ContextCompat.getDrawable(getContext(), android.R.drawable.gallery_thumb)); 138 return v; 139 } 140 } 141 //END_INCLUDE(fragment) 142 } 143