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