• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
onCreate(Bundle savedInstanceState)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         button = (Button)findViewById(R.id.delete_fragment);
48         button.setOnClickListener(new OnClickListener() {
49             public void onClick(View v) {
50                 getFragmentManager().popBackStack();
51             }
52         });
53 
54         if (savedInstanceState == null) {
55             // Do first time initialization -- add initial fragment.
56             Fragment newFragment = CountingFragment.newInstance(mStackLevel);
57             FragmentTransaction ft = getFragmentManager().beginTransaction();
58             ft.add(R.id.simple_fragment, newFragment).commit();
59         } else {
60             mStackLevel = savedInstanceState.getInt("level");
61         }
62     }
63 
64     @Override
onSaveInstanceState(Bundle outState)65     public void onSaveInstanceState(Bundle outState) {
66         super.onSaveInstanceState(outState);
67         outState.putInt("level", mStackLevel);
68     }
69 
70 //BEGIN_INCLUDE(add_stack)
addFragmentToStack()71     void addFragmentToStack() {
72         mStackLevel++;
73 
74         // Instantiate a new fragment.
75         Fragment newFragment = CountingFragment.newInstance(mStackLevel);
76 
77         // Add the fragment to the activity, pushing this transaction
78         // on to the back stack.
79         FragmentTransaction ft = getFragmentManager().beginTransaction();
80         ft.replace(R.id.simple_fragment, newFragment);
81         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
82         ft.addToBackStack(null);
83         ft.commit();
84     }
85 //END_INCLUDE(add_stack)
86 
87 //BEGIN_INCLUDE(fragment)
88     public static class CountingFragment extends Fragment {
89         int mNum;
90 
91         /**
92          * Create a new instance of CountingFragment, providing "num"
93          * as an argument.
94          */
newInstance(int num)95         static CountingFragment newInstance(int num) {
96             CountingFragment f = new CountingFragment();
97 
98             // Supply num input as an argument.
99             Bundle args = new Bundle();
100             args.putInt("num", num);
101             f.setArguments(args);
102 
103             return f;
104         }
105 
106         /**
107          * When creating, retrieve this instance's number from its arguments.
108          */
109         @Override
onCreate(Bundle savedInstanceState)110         public void onCreate(Bundle savedInstanceState) {
111             super.onCreate(savedInstanceState);
112             mNum = getArguments() != null ? getArguments().getInt("num") : 1;
113         }
114 
115         /**
116          * The Fragment's UI is just a simple text view showing its
117          * instance number.
118          */
119         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)120         public View onCreateView(LayoutInflater inflater, ViewGroup container,
121                 Bundle savedInstanceState) {
122             View v = inflater.inflate(R.layout.hello_world, container, false);
123             View tv = v.findViewById(R.id.text);
124             ((TextView)tv).setText("Fragment #" + mNum);
125             tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
126             return v;
127         }
128     }
129 //END_INCLUDE(fragment)
130 }
131