CountingFragment.java revision 3328f87260d3b7be4fcf0c22cdc2dda5d60ab77c
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 */
16package com.example.android.supportv13.app;
17
18import com.example.android.supportv13.R;
19
20import android.app.Fragment;
21import android.os.Bundle;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.TextView;
26
27public class CountingFragment extends Fragment {
28    int mNum;
29
30    /**
31     * Create a new instance of CountingFragment, providing "num"
32     * as an argument.
33     */
34    static CountingFragment newInstance(int num) {
35        CountingFragment f = new CountingFragment();
36
37        // Supply num input as an argument.
38        Bundle args = new Bundle();
39        args.putInt("num", num);
40        f.setArguments(args);
41
42        return f;
43    }
44
45    /**
46     * When creating, retrieve this instance's number from its arguments.
47     */
48    @Override
49    public void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
52    }
53
54    /**
55     * The Fragment's UI is just a simple text view showing its
56     * instance number.
57     */
58    @Override
59    public View onCreateView(LayoutInflater inflater, ViewGroup container,
60            Bundle savedInstanceState) {
61        View v = inflater.inflate(R.layout.counting, container, false);
62        View tv = v.findViewById(R.id.text);
63        ((TextView)tv).setText("Fragment #" + mNum);
64        return v;
65    }
66}
67