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
17package com.example.android.apis.app;
18
19import com.example.android.apis.R;
20
21import android.app.Activity;
22import android.app.Fragment;
23import android.app.FragmentTransaction;
24import android.content.res.TypedArray;
25import android.os.Bundle;
26import android.util.AttributeSet;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.TextView;
31
32/**
33 * Demonstrates a fragment that can be configured through both Bundle arguments
34 * and layout attributes.
35 */
36public class FragmentArguments extends Activity {
37//BEGIN_INCLUDE(create)
38    @Override protected void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40        setContentView(R.layout.fragment_arguments);
41
42        if (savedInstanceState == null) {
43            // First-time init; create fragment to embed in activity.
44            FragmentTransaction ft = getFragmentManager().beginTransaction();
45            Fragment newFragment = MyFragment.newInstance("From Arguments");
46            ft.add(R.id.created, newFragment);
47            ft.commit();
48        }
49    }
50//END_INCLUDE(create)
51
52//BEGIN_INCLUDE(fragment)
53    public static class MyFragment extends Fragment {
54        CharSequence mLabel;
55
56        /**
57         * Create a new instance of MyFragment that will be initialized
58         * with the given arguments.
59         */
60        static MyFragment newInstance(CharSequence label) {
61            MyFragment f = new MyFragment();
62            Bundle b = new Bundle();
63            b.putCharSequence("label", label);
64            f.setArguments(b);
65            return f;
66        }
67
68        /**
69         * Parse attributes during inflation from a view hierarchy into the
70         * arguments we handle.
71         */
72        @Override public void onInflate(Activity activity, AttributeSet attrs,
73                Bundle savedInstanceState) {
74            super.onInflate(activity, attrs, savedInstanceState);
75
76            TypedArray a = activity.obtainStyledAttributes(attrs,
77                    R.styleable.FragmentArguments);
78            mLabel = a.getText(R.styleable.FragmentArguments_android_label);
79            a.recycle();
80        }
81
82        /**
83         * During creation, if arguments have been supplied to the fragment
84         * then parse those out.
85         */
86        @Override public void onCreate(Bundle savedInstanceState) {
87            super.onCreate(savedInstanceState);
88
89            Bundle args = getArguments();
90            if (args != null) {
91                mLabel = args.getCharSequence("label", mLabel);
92            }
93        }
94
95        /**
96         * Create the view for this fragment, using the arguments given to it.
97         */
98        @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
99                Bundle savedInstanceState) {
100            View v = inflater.inflate(R.layout.hello_world, container, false);
101            View tv = v.findViewById(R.id.text);
102            ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
103            tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
104            return v;
105        }
106    }
107//END_INCLUDE(fragment)
108}
109