2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.apis.support.app;
19 import com.example.android.apis.R;
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.FragmentActivity;
23 import android.support.v4.app.FragmentTransaction;
25 import android.os.Bundle;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.View.OnClickListener;
30 import android.widget.Button;
31 import android.widget.TextView;
33 public class FragmentStackSupport extends FragmentActivity {
37 protected void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.fragment_stack);
41 // Watch for button clicks.
42 Button button = (Button)findViewById(R.id.new_fragment);
43 button.setOnClickListener(new OnClickListener() {
44 public void onClick(View v) {
49 if (savedInstanceState == null) {
50 // Do first time initialization -- add initial fragment.
51 Fragment newFragment = CountingFragment.newInstance(mStackLevel);
52 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
53 ft.add(R.id.simple_fragment, newFragment).commit();
55 mStackLevel = savedInstanceState.getInt("level");
60 public void onSaveInstanceState(Bundle outState) {
61 super.onSaveInstanceState(outState);
62 outState.putInt("level", mStackLevel);
65 //BEGIN_INCLUDE(add_stack)
66 void addFragmentToStack() {
69 // Instantiate a new fragment.
70 Fragment newFragment = CountingFragment.newInstance(mStackLevel);
72 // Add the fragment to the activity, pushing this transaction
73 // on to the back stack.
74 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
75 ft.replace(R.id.simple_fragment, newFragment);
76 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
77 ft.addToBackStack(null);
80 //END_INCLUDE(add_stack)
82 //BEGIN_INCLUDE(fragment)
83 public static class CountingFragment extends Fragment {
87 * Create a new instance of CountingFragment, providing "num"
90 static CountingFragment newInstance(int num) {
91 CountingFragment f = new CountingFragment();
93 // Supply num input as an argument.
94 Bundle args = new Bundle();
95 args.putInt("num", num);
102 * When creating, retrieve this instance's number from its arguments.
105 public void onCreate(Bundle savedInstanceState) {
106 super.onCreate(savedInstanceState);
107 mNum = getArguments() != null ? getArguments().getInt("num") : 1;
111 * The Fragment's UI is just a simple text view showing its
115 public View onCreateView(LayoutInflater inflater, ViewGroup container,
116 Bundle savedInstanceState) {
117 View v = inflater.inflate(R.layout.hello_world, container, false);
118 View tv = v.findViewById(R.id.text);
119 ((TextView)tv).setText("Fragment #" + mNum);
120 tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
124 //END_INCLUDE(fragment)