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