Fragment.OnInflate Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| OnInflate(IAttributeSet, Bundle) |
Obsolete.
This overload is deprecated. Override an overload that accepts a host instead. |
| OnInflate(Activity, IAttributeSet, Bundle) |
Obsolete.
This method was deprecated in API level 23. On Android 6.0 and later, override OnInflate(Context, IAttributeSet, Bundle) instead. |
| OnInflate(Context, IAttributeSet, Bundle) |
Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity. |
OnInflate(IAttributeSet, Bundle)
Caution
deprecated
This overload is deprecated. Override an overload that accepts a host instead.
[Android.Runtime.Register("onInflate", "(Landroid/util/AttributeSet;Landroid/os/Bundle;)V", "GetOnInflate_Landroid_util_AttributeSet_Landroid_os_Bundle_Handler")]
[System.Obsolete("deprecated")]
public virtual void OnInflate(Android.Util.IAttributeSet? attrs, Android.OS.Bundle? savedInstanceState);
[<Android.Runtime.Register("onInflate", "(Landroid/util/AttributeSet;Landroid/os/Bundle;)V", "GetOnInflate_Landroid_util_AttributeSet_Landroid_os_Bundle_Handler")>]
[<System.Obsolete("deprecated")>]
abstract member OnInflate : Android.Util.IAttributeSet * Android.OS.Bundle -> unit
override this.OnInflate : Android.Util.IAttributeSet * Android.OS.Bundle -> unit
Parameters
- attrs
- IAttributeSet
AttributeSet
- savedInstanceState
- Bundle
Bundle
- Attributes
Remarks
This overload is deprecated. On Android 6.0 and later, use OnInflate(Context, IAttributeSet, Bundle) instead. On Android 5.0 and 5.1, use OnInflate(Activity, IAttributeSet, Bundle).
If you override this method you must call through to the superclass implementation.
Android reference for android.app.Fragment.onInflate.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Applies to
OnInflate(Activity, IAttributeSet, Bundle)
Caution
deprecated
This method was deprecated in API level 23. On Android 6.0 and later, override OnInflate(Context, IAttributeSet, Bundle) instead.
[Android.Runtime.Register("onInflate", "(Landroid/app/Activity;Landroid/util/AttributeSet;Landroid/os/Bundle;)V", "GetOnInflate_Landroid_app_Activity_Landroid_util_AttributeSet_Landroid_os_Bundle_Handler")]
[System.Obsolete("deprecated")]
public virtual void OnInflate(Android.App.Activity? activity, Android.Util.IAttributeSet? attrs, Android.OS.Bundle? savedInstanceState);
[<Android.Runtime.Register("onInflate", "(Landroid/app/Activity;Landroid/util/AttributeSet;Landroid/os/Bundle;)V", "GetOnInflate_Landroid_app_Activity_Landroid_util_AttributeSet_Landroid_os_Bundle_Handler")>]
[<System.Obsolete("deprecated")>]
abstract member OnInflate : Android.App.Activity * Android.Util.IAttributeSet * Android.OS.Bundle -> unit
override this.OnInflate : Android.App.Activity * Android.Util.IAttributeSet * Android.OS.Bundle -> unit
Parameters
- activity
- Activity
Activity
- attrs
- IAttributeSet
AttributeSet
- savedInstanceState
- Bundle
Bundle
- Attributes
Remarks
This method was deprecated in API level 23. On Android 6.0 and later, use OnInflate(Context, IAttributeSet, Bundle) instead. Android 5.0 and 5.1 call this overload.
If you override this method you must call through to the superclass implementation.
Android reference for android.app.Fragment.onInflate.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Applies to
OnInflate(Context, IAttributeSet, Bundle)
Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.
[Android.Runtime.Register("onInflate", "(Landroid/content/Context;Landroid/util/AttributeSet;Landroid/os/Bundle;)V", "GetOnInflate_Landroid_content_Context_Landroid_util_AttributeSet_Landroid_os_Bundle_Handler", ApiSince=23)]
public virtual void OnInflate(Android.Content.Context? context, Android.Util.IAttributeSet? attrs, Android.OS.Bundle? savedInstanceState);
[<Android.Runtime.Register("onInflate", "(Landroid/content/Context;Landroid/util/AttributeSet;Landroid/os/Bundle;)V", "GetOnInflate_Landroid_content_Context_Landroid_util_AttributeSet_Landroid_os_Bundle_Handler", ApiSince=23)>]
abstract member OnInflate : Android.Content.Context * Android.Util.IAttributeSet * Android.OS.Bundle -> unit
override this.OnInflate : Android.Content.Context * Android.Util.IAttributeSet * Android.OS.Bundle -> unit
Parameters
- context
- Context
The Context that is inflating this fragment.
- attrs
- IAttributeSet
The attributes at the tag where the fragment is being created.
- savedInstanceState
- Bundle
If the fragment is being re-created from a previous saved state, this is the state.
- Attributes
Remarks
Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity. This may be called immediately after the fragment is created from a tag in a layout file. This is before the fragment's OnAttach(Context) has been called; parse the attributes and save them here. It is called for a new instance, including one created from saved state, but not when an existing instance is retained. Re-parse the parameters when this method is called to allow them to change with different configurations.
Here is a typical implementation of a fragment that can take parameters both through attributes supplied here as well from getArguments():
public static class MyFragment extends Fragment {
CharSequence mLabel;
/**
* Create a new instance of MyFragment that will be initialized
* with the given arguments.
*/
static MyFragment newInstance(CharSequence label) {
MyFragment f = new MyFragment();
Bundle b = new Bundle();
b.putCharSequence("label", label);
f.setArguments(b);
return f;
}
/**
* Parse attributes during inflation from a view hierarchy into the
* arguments we handle.
*/
@Override public void onInflate(Context context, AttributeSet attrs,
Bundle savedInstanceState) {
super.onInflate(context, attrs, savedInstanceState);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FragmentArguments);
mLabel = a.getText(R.styleable.FragmentArguments_android_label);
a.recycle();
}
/**
* During creation, if arguments have been supplied to the fragment
* then parse those out.
*/
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
mLabel = args.getCharSequence("label", mLabel);
}
}
/**
* Create the view for this fragment, using the arguments given to it.
*/
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}
Note that parsing the XML attributes uses a "styleable" resource. The declaration for the styleable used here is:
<declare-styleable name="FragmentArguments">
<attr name="android:label" />
</declare-styleable>
The fragment can then be declared within its activity's content layout through a tag like this:
<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
android:id="@+id/embedded"
android:layout_width="0px" android:layout_height="wrap_content"
android:layout_weight="1"
android:label="@string/fragment_arguments_embedded" />
This fragment can also be created dynamically from arguments given at runtime in the arguments Bundle; here is an example of doing so at creation of the containing activity:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_arguments);
if (savedInstanceState == null) {
// First-time init; create fragment to embed in activity.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment newFragment = MyFragment.newInstance("From Arguments");
ft.add(R.id.created, newFragment);
ft.commit();
}
}
Android reference for android.app.Fragment.onInflate.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.