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