OSDN Git Service

Add new API demo for -swNNNdp resource qualifier.
[android-x86/development.git] / samples / ApiDemos / src / com / example / android / apis / support / app / FragmentStackSupport.java
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.apis.support.app;
18
19 import com.example.android.apis.R;
20
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.FragmentActivity;
23 import android.support.v4.app.FragmentTransaction;
24
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;
32
33 public class FragmentStackSupport extends FragmentActivity {
34     int mStackLevel = 1;
35
36     @Override
37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         setContentView(R.layout.fragment_stack);
40
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) {
45                 addFragmentToStack();
46             }
47         });
48
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();
54         } else {
55             mStackLevel = savedInstanceState.getInt("level");
56         }
57     }
58
59     @Override
60     public void onSaveInstanceState(Bundle outState) {
61         super.onSaveInstanceState(outState);
62         outState.putInt("level", mStackLevel);
63     }
64
65 //BEGIN_INCLUDE(add_stack)
66     void addFragmentToStack() {
67         mStackLevel++;
68
69         // Instantiate a new fragment.
70         Fragment newFragment = CountingFragment.newInstance(mStackLevel);
71
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);
78         ft.commit();
79     }
80 //END_INCLUDE(add_stack)
81
82 //BEGIN_INCLUDE(fragment)
83     public static class CountingFragment extends Fragment {
84         int mNum;
85
86         /**
87          * Create a new instance of CountingFragment, providing "num"
88          * as an argument.
89          */
90         static CountingFragment newInstance(int num) {
91             CountingFragment f = new CountingFragment();
92
93             // Supply num input as an argument.
94             Bundle args = new Bundle();
95             args.putInt("num", num);
96             f.setArguments(args);
97
98             return f;
99         }
100
101         /**
102          * When creating, retrieve this instance's number from its arguments.
103          */
104         @Override
105         public void onCreate(Bundle savedInstanceState) {
106             super.onCreate(savedInstanceState);
107             mNum = getArguments() != null ? getArguments().getInt("num") : 1;
108         }
109
110         /**
111          * The Fragment's UI is just a simple text view showing its
112          * instance number.
113          */
114         @Override
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));
121             return v;
122         }
123     }
124 //END_INCLUDE(fragment)
125 }