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