java.lang.Object | |
↳ | android.support.v4.app.Fragment |
Known Direct Subclasses |
Static library support version of the framework's Fragment
.
Used to write apps that run on platforms prior to Android 3.0. When running
on Android 3.0 or above, this implementation is still used; it does not try
to switch to the framework's implementation. See the framework SDK
documentation for a class overview.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Fragment.InstantiationException | Thrown by instantiate(Context, String, Bundle) when
there is an instantiation failure. |
||||||||||
Fragment.SavedState | State information that has been retrieved from a fragment instance
through FragmentManager.saveFragmentInstanceState . |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Default constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Print the Fragments's state into the given stream.
| |||||||||||
Subclasses can not override equals().
| |||||||||||
Return the Activity this fragment is currently associated with.
| |||||||||||
Return the arguments supplied when the fragment was instantiated,
if any.
| |||||||||||
Return a private FragmentManager for placing and managing Fragments
inside of this Fragment.
| |||||||||||
Return the FragmentManager for interacting with fragments associated
with this fragment's activity.
| |||||||||||
Return the identifier this fragment is known by.
| |||||||||||
Return the LoaderManager for this fragment, creating it if needed.
| |||||||||||
Returns the parent Fragment containing this Fragment.
| |||||||||||
Return
getActivity().getResources() . | |||||||||||
Return a localized string from the application's package's
default string table.
| |||||||||||
Return a localized formatted string from the application's package's
default string table, substituting the format arguments as defined in
Formatter and format(String, Object...) . | |||||||||||
Get the tag name of the fragment, if specified.
| |||||||||||
Return the target fragment set by
setTargetFragment(Fragment, int) . | |||||||||||
Return the target request code set by
setTargetFragment(Fragment, int) . | |||||||||||
Return a localized, styled CharSequence from the application's package's
default string table.
| |||||||||||
Get the root view for the fragment's layout (the one returned by
onCreateView(LayoutInflater, ViewGroup, Bundle) ),
if provided. | |||||||||||
Subclasses can not override hashCode().
| |||||||||||
Like
instantiate(Context, String, Bundle) but with a null
argument Bundle. | |||||||||||
Create a new instance of a Fragment with the given class name.
| |||||||||||
Return true if the fragment is currently added to its activity.
| |||||||||||
Return true if the fragment has been explicitly detached from the UI.
| |||||||||||
Return true if the fragment has been hidden.
| |||||||||||
Return true if the layout is included as part of an activity view
hierarchy via the <fragment> tag.
| |||||||||||
Return true if this fragment is currently being removed from its
activity.
| |||||||||||
Return true if the fragment is in the resumed state.
| |||||||||||
Return true if the fragment is currently visible to the user.
| |||||||||||
Called when the fragment's activity has been created and this
fragment's view hierarchy instantiated.
| |||||||||||
Receive the result from a previous call to
startActivityForResult(Intent, int) . | |||||||||||
Called when a fragment is first attached to its activity.
| |||||||||||
Called by the system when the device configuration changes while your
component is running.
| |||||||||||
This hook is called whenever an item in a context menu is selected.
| |||||||||||
Called to do initial creation of a fragment.
| |||||||||||
Called when a fragment loads an animation.
| |||||||||||
Called when a context menu for the
view is about to be shown. | |||||||||||
Initialize the contents of the Activity's standard options menu.
| |||||||||||
Called to have the fragment instantiate its user interface view.
| |||||||||||
Called when the fragment is no longer in use.
| |||||||||||
Called when this fragment's option menu items are no longer being
included in the overall options menu.
| |||||||||||
Called when the view previously created by
onCreateView(LayoutInflater, ViewGroup, Bundle) has
been detached from the fragment. | |||||||||||
Called when the fragment is no longer attached to its activity.
| |||||||||||
Called when the hidden state (as returned by
isHidden() of
the fragment has changed. | |||||||||||
Called when a fragment is being created as part of a view layout
inflation, typically from setting the content view of an activity.
| |||||||||||
This is called when the overall system is running low on memory, and
would like actively running process to try to tighten their belt.
| |||||||||||
This hook is called whenever an item in your options menu is selected.
| |||||||||||
This hook is called whenever the options menu is being closed (either by the user canceling
the menu with the back/menu button, or when an item is selected).
| |||||||||||
Called when the Fragment is no longer resumed.
| |||||||||||
Prepare the Screen's standard options menu to be displayed.
| |||||||||||
Called when the fragment is visible to the user and actively running.
| |||||||||||
Called to ask the fragment to save its current dynamic state, so it
can later be reconstructed in a new instance of its process is
restarted.
| |||||||||||
Called when the Fragment is visible to the user.
| |||||||||||
Called when the Fragment is no longer started.
| |||||||||||
Called immediately after
onCreateView(LayoutInflater, ViewGroup, Bundle)
has returned, but before any saved state has been restored in to the view. | |||||||||||
Called when all saved state has been restored into the view hierarchy
of the fragment.
| |||||||||||
Registers a context menu to be shown for the given view (multiple views
can show the context menu).
| |||||||||||
Supply the construction arguments for this fragment.
| |||||||||||
Report that this fragment would like to participate in populating
the options menu by receiving a call to
onCreateOptionsMenu(Menu, MenuInflater)
and related methods. | |||||||||||
Set the initial saved state that this Fragment should restore itself
from when first being constructed, as returned by
FragmentManager.saveFragmentInstanceState . | |||||||||||
Set a hint for whether this fragment's menu should be visible.
| |||||||||||
Control whether a fragment instance is retained across Activity
re-creation (such as from a configuration change).
| |||||||||||
Optional target for this fragment.
| |||||||||||
Set a hint to the system about whether this fragment's UI is currently visible
to the user.
| |||||||||||
Call
startActivity(Intent) on the fragment's
containing Activity. | |||||||||||
Call
startActivityForResult(Intent, int) on the fragment's
containing Activity. | |||||||||||
Returns a string containing a concise, human-readable description of this
object.
| |||||||||||
Prevents a context menu to be shown for the given view.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
android.content.ComponentCallbacks
| |||||||||||
From interface
android.view.View.OnCreateContextMenuListener
|
Default constructor. Every fragment must have an
empty constructor, so it can be instantiated when restoring its
activity's state. It is strongly recommended that subclasses do not
have other constructors with parameters, since these constructors
will not be called when the fragment is re-instantiated; instead,
arguments can be supplied by the caller with setArguments(Bundle)
and later retrieved by the Fragment with getArguments()
.
Applications should generally not implement a constructor. The
first place application code an run where the fragment is ready to
be used is in onAttach(Activity)
, the point where the fragment
is actually associated with its activity. Some applications may also
want to implement onInflate(Activity, AttributeSet, Bundle)
to retrieve attributes from a
layout resource, though should take care here because this happens for
the fragment is attached to its activity.
Print the Fragments's state into the given stream.
prefix | Text to print at the front of each line. |
---|---|
fd | The raw file descriptor that the dump is being sent to. |
writer | The PrintWriter to which you should dump your state. This will be closed for you after you return. |
args | additional arguments to the dump request. |
Subclasses can not override equals().
o | the object to compare this instance with. |
---|
true
if the specified object is equal to this Object
; false
otherwise.Return the Activity this fragment is currently associated with.
Return the arguments supplied when the fragment was instantiated, if any.
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
Return the FragmentManager for interacting with fragments associated
with this fragment's activity. Note that this will be non-null slightly
before getActivity()
, during the time from when the fragment is
placed in a FragmentTransaction
until it is committed and
attached to its activity.
If this Fragment is a child of another Fragment, the FragmentManager
returned here will be the parent's getChildFragmentManager()
.
Return the identifier this fragment is known by. This is either the android:id value supplied in a layout or the container view ID supplied when adding the fragment.
Return the LoaderManager for this fragment, creating it if needed.
Returns the parent Fragment containing this Fragment. If this Fragment is attached directly to an Activity, returns null.
Return a localized string from the application's package's default string table.
resId | Resource id for the string |
---|
Return a localized formatted string from the application's package's
default string table, substituting the format arguments as defined in
Formatter
and format(String, Object...)
.
resId | Resource id for the format string |
---|---|
formatArgs | The format arguments that will be used for substitution. |
Return the target fragment set by setTargetFragment(Fragment, int)
.
Return the target request code set by setTargetFragment(Fragment, int)
.
Return a localized, styled CharSequence from the application's package's default string table.
resId | Resource id for the CharSequence text |
---|
Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)
),
if provided.
Subclasses can not override hashCode().
Like instantiate(Context, String, Bundle)
but with a null
argument Bundle.
Create a new instance of a Fragment with the given class name. This is the same as calling its empty constructor.
context | The calling context being used to instantiate the fragment. This is currently just used to get its ClassLoader. |
---|---|
fname | The class name of the fragment to instantiate. |
args | Bundle of arguments to supply to the fragment, which it
can retrieve with getArguments() . May be null. |
InstantiationException | If there is a failure in instantiating the given fragment class. This is a runtime exception; it is not normally expected to happen. |
---|
Return true if the fragment is currently added to its activity.
Return true if the fragment has been explicitly detached from the UI.
That is, FragmentTransaction.detach(Fragment)
has been used on it.
Return true if the fragment has been hidden. By default fragments
are shown. You can find out about changes to this state with
onHiddenChanged(boolean)
. Note that the hidden state is orthogonal
to other states -- that is, to be visible to the user, a fragment
must be both started and not hidden.
Return true if the layout is included as part of an activity view hierarchy via the <fragment> tag. This will always be true when fragments are created through the <fragment> tag, except in the case where an old fragment is restored from a previous state and it does not appear in the layout of the current state.
Return true if this fragment is currently being removed from its activity. This is not whether its activity is finishing, but rather whether it is in the process of being removed from its activity.
Return true if the fragment is in the resumed state. This is true
for the duration of onResume()
and onPause()
as well.
Return true if the fragment is currently visible to the user. This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden.
Called when the fragment's activity has been created and this
fragment's view hierarchy instantiated. It can be used to do final
initialization once these pieces are in place, such as retrieving
views or restoring state. It is also useful for fragments that use
setRetainInstance(boolean)
to retain their instance,
as this callback tells the fragment when it is fully associated with
the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle)
and before onViewStateRestored(Bundle)
.
savedInstanceState | If the fragment is being re-created from a previous saved state, this is the state. |
---|
Receive the result from a previous call to
startActivityForResult(Intent, int)
. This follows the
related Activity API as described there in
onActivityResult(int, int, Intent)
.
requestCode | The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from. |
---|---|
resultCode | The integer result code returned by the child activity through its setResult(). |
data | An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). |
Called when a fragment is first attached to its activity.
onCreate(Bundle)
will be called after this.
Called by the system when the device configuration changes while your component is running. Note that, unlike activities, other components are never restarted when a configuration changes: they must always deal with the results of the change, such as by re-retrieving resources.
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
newConfig | The new device configuration. |
---|
This hook is called whenever an item in a context menu is selected. The default implementation simply returns false to have the normal processing happen (calling the item's Runnable or sending a message to its Handler as appropriate). You can use this method for any items for which you would like to do processing without those other facilities.
Use getMenuInfo()
to get extra information set by the
View that added this menu item.
Derived classes should call through to the base class for it to perform the default menu handling.
item | The context menu item that was selected. |
---|
Called to do initial creation of a fragment. This is called after
onAttach(Activity)
and before
onCreateView(LayoutInflater, ViewGroup, Bundle)
.
Note that this can be called while the fragment's activity is
still in the process of being created. As such, you can not rely
on things like the activity's content view hierarchy being initialized
at this point. If you want to do work once the activity itself is
created, see onActivityCreated(Bundle)
.
savedInstanceState | If the fragment is being re-created from a previous saved state, this is the state. |
---|
Called when a fragment loads an animation.
Called when a context menu for the view
is about to be shown.
Unlike onCreateOptionsMenu(Menu, MenuInflater)
, this will be called every
time the context menu is about to be shown and should be populated for
the view (or item inside the view for AdapterView
subclasses,
this can be found in the menuInfo
)).
Use onContextItemSelected(android.view.MenuItem)
to know when an
item has been selected.
The default implementation calls up to
Activity.onCreateContextMenu
, though
you can not call this implementation if you don't want that behavior.
It is not safe to hold onto the context menu after this method returns. Called when the context menu for this view is being built. It is not safe to hold onto the menu after this method returns.
menu | The context menu that is being built |
---|---|
v | The view for which the context menu is being built |
menuInfo | Extra information about the item for which the context menu should be shown. This information will vary depending on the class of v. |
Initialize the contents of the Activity's standard options menu. You
should place your menu items in to menu. For this method
to be called, you must have first called setHasOptionsMenu(boolean)
. See
Activity.onCreateOptionsMenu
for more information.
menu | The options menu in which you place your items. |
---|
Called to have the fragment instantiate its user interface view.
This is optional, and non-graphical fragments can return null (which
is the default implementation). This will be called between
onCreate(Bundle)
and onActivityCreated(Bundle)
.
If you return a View from here, you will later be called in
onDestroyView()
when the view is being released.
inflater | The LayoutInflater object that can be used to inflate any views in the fragment, |
---|---|
container | If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view. |
savedInstanceState | If non-null, this fragment is being re-constructed from a previous saved state as given here. |
Called when the fragment is no longer in use. This is called
after onStop()
and before onDetach()
.
Called when this fragment's option menu items are no longer being
included in the overall options menu. Receiving this call means that
the menu needed to be rebuilt, but this fragment's items were not
included in the newly built menu (its onCreateOptionsMenu(Menu, MenuInflater)
was not called).
Called when the view previously created by onCreateView(LayoutInflater, ViewGroup, Bundle)
has
been detached from the fragment. The next time the fragment needs
to be displayed, a new view will be created. This is called
after onStop()
and before onDestroy()
. It is called
regardless of whether onCreateView(LayoutInflater, ViewGroup, Bundle)
returned a
non-null view. Internally it is called after the view's state has
been saved but before it has been removed from its parent.
Called when the fragment is no longer attached to its activity. This
is called after onDestroy()
.
Called when the hidden state (as returned by isHidden()
of
the fragment has changed. Fragments start out not hidden; this will
be called whenever the fragment changes state from that.
hidden | True if the fragment is now hidden, false if it is not visible. |
---|
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 This is called every time the fragment is inflated, even if it is
being inflated into a new instance with saved state. It typically makes
sense to re-parse the parameters each time, 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 Note that parsing the XML attributes uses a "styleable" resource. The
declaration for the styleable used here is: The fragment can then be declared within its activity's content layout
through a tag like this: 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:onAttach(Activity)
has been called; all you should do here is
parse the attributes and save them away.
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(Activity activity, AttributeSet attrs,
Bundle savedInstanceState) {
super.onInflate(activity, attrs, savedInstanceState);
TypedArray a = activity.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;
}
}
<declare-styleable name="FragmentArguments">
<attr name="android:label" />
</declare-styleable>
<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" />
@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();
}
}
activity | The Activity that is inflating this fragment. |
---|---|
attrs | The attributes at the tag where the fragment is being created. |
savedInstanceState | If the fragment is being re-created from a previous saved state, this is the state. |
This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt. While the exact point at which this will be called is not defined, generally it will happen around the time all background process have been killed, that is before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.
Applications that want to be nice can implement this method to release any caches or other unnecessary resources they may be holding on to. The system will perform a gc for you after returning from this method.
This hook is called whenever an item in your options menu is selected. The default implementation simply returns false to have the normal processing happen (calling the item's Runnable or sending a message to its Handler as appropriate). You can use this method for any items for which you would like to do processing without those other facilities.
Derived classes should call through to the base class for it to perform the default menu handling.
item | The menu item that was selected. |
---|
This hook is called whenever the options menu is being closed (either by the user canceling the menu with the back/menu button, or when an item is selected).
menu | The options menu as last shown or first initialized by onCreateOptionsMenu(). |
---|
Called when the Fragment is no longer resumed. This is generally
tied to Activity.onPause
of the containing
Activity's lifecycle.
Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents. See
Activity.onPrepareOptionsMenu
for more information.
menu | The options menu as last shown or first initialized by onCreateOptionsMenu(). |
---|
Called when the fragment is visible to the user and actively running.
This is generally
tied to Activity.onResume
of the containing
Activity's lifecycle.
Called to ask the fragment to save its current dynamic state, so it
can later be reconstructed in a new instance of its process is
restarted. If a new instance of the fragment later needs to be
created, the data you place in the Bundle here will be available
in the Bundle given to onCreate(Bundle)
,
onCreateView(LayoutInflater, ViewGroup, Bundle)
, and
onActivityCreated(Bundle)
.
This corresponds to Activity.onSaveInstanceState(Bundle)
and most of the discussion there
applies here as well. Note however: this method may be called
at any time before onDestroy()
. There are many situations
where a fragment may be mostly torn down (such as when placed on the
back stack with no UI showing), but its state will not be saved until
its owning activity actually needs to save its state.
outState | Bundle in which to place your saved state. |
---|
Called when the Fragment is visible to the user. This is generally
tied to Activity.onStart
of the containing
Activity's lifecycle.
Called when the Fragment is no longer started. This is generally
tied to Activity.onStop
of the containing
Activity's lifecycle.
Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle)
has returned, but before any saved state has been restored in to the view.
This gives subclasses a chance to initialize themselves once
they know their view hierarchy has been completely created. The fragment's
view hierarchy is not however attached to its parent at this point.
view | The View returned by onCreateView(LayoutInflater, ViewGroup, Bundle) . |
---|---|
savedInstanceState | If non-null, this fragment is being re-constructed from a previous saved state as given here. |
Called when all saved state has been restored into the view hierarchy
of the fragment. This can be used to do initialization based on saved
state that you are letting the view hierarchy track itself, such as
whether check box widgets are currently checked. This is called
after onActivityCreated(Bundle)
and before
onStart()
.
savedInstanceState | If the fragment is being re-created from a previous saved state, this is the state. |
---|
Registers a context menu to be shown for the given view (multiple views
can show the context menu). This method will set the
View.OnCreateContextMenuListener
on the view to this fragment, so
onCreateContextMenu(ContextMenu, View, ContextMenuInfo)
will be
called when it is time to show the context menu.
view | The view that should show a context menu. |
---|
Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation.
Report that this fragment would like to participate in populating
the options menu by receiving a call to onCreateOptionsMenu(Menu, MenuInflater)
and related methods.
hasMenu | If true, the fragment has menu items to contribute. |
---|
Set the initial saved state that this Fragment should restore itself
from when first being constructed, as returned by
FragmentManager.saveFragmentInstanceState
.
state | The state the fragment should be restored from. |
---|
Set a hint for whether this fragment's menu should be visible. This is useful if you know that a fragment has been placed in your view hierarchy so that the user can not currently seen it, so any menu items it has should also not be shown.
menuVisible | The default is true, meaning the fragment's menu will be shown as usual. If false, the user will not see the menu. |
---|
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:
onDestroy()
will not be called (but onDetach()
still
will be, because the fragment is being detached from its current activity).
onCreate(Bundle)
will not be called since the fragment
is not being re-created.
onAttach(Activity)
and onActivityCreated(Bundle)
will
still be called.
Optional target for this fragment. This may be used, for example,
if this fragment is being started by another, and when done wants to
give a result back to the first. The target set here is retained
across instances via FragmentManager.putFragment()
.
fragment | The fragment that is the target of this one. |
---|---|
requestCode | Optional request code, for convenience if you
are going to call back with onActivityResult(int, int, Intent) .
|
Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.
An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.
isVisibleToUser | true if this fragment's UI is currently visible to the user (default), false if it is not. |
---|
Call startActivity(Intent)
on the fragment's
containing Activity.
Call startActivityForResult(Intent, int)
on the fragment's
containing Activity.
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString
method
if you intend implementing your own toString
method.
Prevents a context menu to be shown for the given view. This method will
remove the View.OnCreateContextMenuListener
on the view.
view | The view that should stop showing a context menu. |
---|