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