Home | History | Annotate | Download | only in app
      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 package com.example.android.supportv13.app;
     17 
     18 import android.app.Fragment;
     19 import android.os.Bundle;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.TextView;
     24 
     25 import com.example.android.supportv13.R;
     26 
     27 public 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